Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • 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
    • DesignFast
  • 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.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

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

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • 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
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • 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
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe