Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to design an Arduino library for an 8-bit IO port

By Ashutosh Bhatt February 6, 2024

In this project, we’ll create an 8-bit IO port Arduino library that reads and writes all eight bits in a single command by combining Arduino’s pins. This means sending and receiving the 8-bit data from a single pin will be possible.

Arduino provides digital output using the digitalWrite() function and receives digital input via the digitalRead() function. By using these two functions, Arduino can give output or input from any single pin. However, Arduino typically only provides input or output from one pin at a time. 

So, to interface Arduino with an 8-bit IO device — such as a 7-segment display, DIP switches, or DAC (digital-to-analog converter), eight different pins are usually required. The data byte (8-bit) pattern is sent to different pins as ‘1s’ and ‘0s.’ For example, if a data byte is 37h (0011 0111), it’s necessary to send the ‘0’ and ‘1’ pattern to eight different pins using the digitalWrite() function.   

To resolve this situation, we created an 8-bit IO port (input-output port) library that combines Arduino’s pins. It works as an 8-bit IO port for Arduino and configures its data direction as input or output. The data direction is set by the character ‘O’ for the output and ‘I’ for the input. 

This library has five functions. Two ” constructors ” create the port object(s), including one function that sends the 8-bit digital output to the port pins and another that receives the 8-bit digital input from the port pins. A third function alters and sets the port’s IO direction. All five functions are explained next.

The five functions

1. IO_Port_8bit(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8,char dir)
This constructor creates object(s) of this class. It can make one or several 8-bit port(s) by combining specific Arduino pins. It’s important to specify which eight Arduino pins to combine, along with the data direction of each port (the input or output). The last argument, dir, defines whether a port works as input or output. 

  •  If the dir=’O’ = the port works as output
  • If the dir=’I’ = the port works as input

 The same port cannot work as input and output simultaneously or alternatively. The port’s direction must be selected or it will result in an error.

2. IO_Port_8bit(int pin1,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8)
This is another constructor, which creates object(s) of this class. It can make one or several 8-bit port(s) by combining specific Arduino pins. It’s important to specify which eight Arduino pins to combine for each port. However, including the data direction as input or output is unnecessary.

Instead, after creating a port object using this constructor, set the port direction using the set_IO_direction function. This constructor allows the programmer to alter the direction of port data in run time. So, a port can work as input or output alternatively (but not simultaneously).

3. set_IO_direction(char dir)
This function specifies the input and output direction of a port. It has one character argument, which is ‘I’ for a port that’s input and ‘O’ for a port that’s output. An error will be displayed on Arduino’s serial monitor if the data direction is not selected.

4. send_8bit_data(int byt)
This function sends 8-bit data to specified pins. It provides the ‘int’ data (must be < 255) as an argument, which is sent directly to eight different pins. If data is >255, an error will be displayed on Arduino’s serial monitor.

5. get_8bit_data(void)
This function receives 8-bit data from specified pins. It returns the 8-bit ‘int’ data by reading the status of the eight different pins.

Example 1: Blink eight LEDs alternatively at a rate of 1 Hz

#include<IO_Port_8bit.h>

IO_Port_8bit myport(2,3,4,5,6,7,8,9,’O’); //create output port
void setup()                             // nothing required in setup
{   
}
void loop()
{
  myport.send_8bit_data(85);// send data to blink all odd LEDs
  delay(500);
  myport.send_8bit_data(170); send data to blink all even LEDs
  delay(500);
}

Circuit diagram

Example 2: Display a binary counting pattern on the LEDs, from 0 to F

#include <IO_Port_8bit.h>
IO_Port_8bit my8bitport(2,3,4,5,6,7,8,9);//create port object
void setup()
{
  my8bitport.set_IO_direction(‘O’);// set port direction
}

void loop()
{
  int i;
  for(i=0;i<16;i++)                    //send data 0 to 15 to display
    {                                                 // binary pattern
     myport.send_8bit_data(i);
     delay(200);
    }
  }

Example 3: Indicate the analog input voltage level on an 8-bit LED bar graph display

#include<IO_Port_8bit.h>

IO_Port_8bit myport(2,3,4,5,6,7,8,9,’O’); //create output port object
void setup()
{
  myport.send_8bit_data(255);          //blink all LEDs of bar graph once
  delay(500);
  myport.send_8bit_data(0);
}

void loop()
{
   int level;
   level = analogRead(A0);       //read analog input voltage
   level = map(level,0,1023,0,80);// limit the voltage from 0 to 80
    //increase or decrease bar graph level as per input
     if((level<80) && (level>70)) myport.send_8bit_data(255);
     else if((level<=70) && (level>60)) myport.send_8bit_data(127);
     else if((level<=60) && (level>50)) myport.send_8bit_data(63);
     else if((level<=50) && (level>40)) myport.send_8bit_data(31);
     else if((level<=40) && (level>30)) myport.send_8bit_data(15);
     else if((level<=30) && (level>20)) myport.send_8bit_data(7);
     else if((level<=20) && (level>10))myport.send_8bit_data(3);
     else if((level<=10) && (level>0)) myport.send_8bit_data(1);
     else if(level==0) myport.send_8bit_data(0);   

}

Circuit diagram

Example 4: Receive an 8-bit digital input from DIP switches and display value on serial monitor

#include<IO_Port_8bit.h>

IO_Port_8bit myport(2,3,4,5,6,7,8,9); //create port object
void setup()
{
    Serial.begin(9600);                            // initialize serial commu.
    myport.set_IO_direction(‘I’);                      // set port direction
    Serial.println(“8-bit input port test”);
}

void loop()
{
  int input_byt;
  input_byt = myport.get_8bit_data();  // read the DIP switch status
  Serial.print(“received input = “);   // and
  Serial.println(input_byt);                    // display its value
  delay(1000);
  }

Circuit diagram

 

You may also like:


  • How to make HTTP requests using Arduino for the IoT

  • How to convert Arduino into a Modbus device

  • How to use Arduino’s analog comparator

  • What are the top tools for developing embedded software?

  • How to schedule embedded tasks in Arduino using FreeRTOS

Filed Under: Arduino Projects, Electronic Projects
Tagged With: Arduino, electronicprojects, libraries, pins
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions.

  • What is the frequency changing when I change the W?
  • PDKs setup for Virtuoso
  • Why do expensive synchronous rectifier controllers exist?
  • improving in delivering a signal from source to load
  • rechargeable battery and simple alkaline battery in one single product

RSS Electro-Tech-Online.com Discussions

  • Manually actuate fuel tank selector solenoid
  • JLCPBC are using a different shipping company = less $$$$$$$$
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS
  • need two ICs
  • MLT-8530 Buzzer on ESP32: Why Is the Sound Output Lower Than Expected?

Featured – Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
  • A filter design example
  • Types of filter responses
  • What are the two types of hardware filters?
  • What are hardware filters and their types?

Recent Articles

  • GigaDevices introduces 32-bit MCUs with integrated DSP and FPU support
  • Grinn introduces 8-core SBC supporting AI-ready embedded development
  • EPC’s 100 kHz BLDC inverter supports high-efficiency motion control
  • Melexis announces 5 W smart driver to supports sensorless FOC operation
  • STMicroelectronics’ motion sensor simplifies industrial IoT system design

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe