Digital Pins To Byte Variable... - Arduino Forum

Digital Pins to Byte Variable... Projects Programming June 10, 2016, 9:07pm 1

I have 8 digital pins on an Arduino connected to other hardware, as a way to communicate a number from 0-255 to the Arduino. What's an efficient way of checking those digital inputs and converting them into a single byte held in a variable?

I've know and can check the status of each bit one at a time and add it all up into a variable, but that seems to laborious and inefficient. Seems like there should be a way to set individual bits within a byte variable based on each digital input's status, or even read a byte value into a variable all at once based on the status of 8 digital inputs. Any ideas/help?

June 10, 2016, 9:14pm 2

Seems like there should be a way to set individual bits within a byte variable based on each digital input's status

Like using bitWrite()?

or even read a byte value into a variable all at once based on the status of 8 digital inputs

If they are all on the same port, you can.

June 10, 2016, 10:25pm 3

If you are using an Arduino UNO it will be a little hard to find 8 pins connected to the same port. The only port that kas all eight bits connected to pins is PORTD which covers Pin 0 through Pin 7. Unfortunately that includes Pin 0 and Pin 1 which are used for Serial. If you use them for data you can't use them for Serial.

The Arduino MEGA 2560 has several choices: 22-29 = PORTA 37-30 = PORTC (note the reversed ordering) 48-42 = PORTL (note the reversed ordering) 54-61 = PORTF 62-69 = PORTK

June 10, 2016, 11:22pm 4

The closest I've been able to do on the 328P chip is to use the upper four bits of port D, pins 4 - 7, for the high nibble and the lower four bits of port B, pins 8 - 11, as the low nibble. By reading each port with a mask and adding them together, you will get one byte. If you need to use SPI then use port C, pins A0 - A3, for the low nibble. That also leaves the I2C pins available. It's not elegant but a lot faster than using bitRead and digitalRead.

June 11, 2016, 12:09am 5

ArduinoTom: I've know and can check the status of each bit one at a time and add it all up into a variable, but that seems to laborious and inefficient.

How much free time does the processor have, in your application?

June 11, 2016, 3:24am 6

johnwasser: If you are using an Arduino UNO it will be a little hard to find 8 pins connected to the same port. The only port that kas all eight bits connected to pins is PORTD which covers Pin 0 through Pin 7. Unfortunately that includes Pin 0 and Pin 1 which are used for Serial. If you use them for data you can't use them for Serial.

The Arduino MEGA 2560 has several choices: 22-29 = PORTA 37-30 = PORTC (note the reversed ordering) 48-42 = PORTL (note the reversed ordering) 54-61 = PORTF 62-69 = PORTK

Arctic_Eddie: The closest I've been able to do on the 328P chip is to use the upper four bits of port D, pins 4 - 7, for the high nibble and the lower four bits of port B, pins 8 - 11, as the low nibble. By reading each port with a mask and adding them together, you will get one byte. If you need to use SPI then use port C, pins A0 - A3, for the low nibble. That also leaves the I2C pins available. It's not elegant but a lot faster than using bitRead and digitalRead.

Sorry, I'm missing the mechanics on this. I'm not following how to read a nibble all at once from a Port, or for that matter how to read a byte if I had an entire Port available... Could you dumb it down for me? :slight_smile:

June 11, 2016, 5:31am 7

Sorry, I'm missing the mechanics on this.

Arduino Reference - Arduino Reference

The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

June 11, 2016, 4:57pm 8

Thank, that gets me into a

aarg: How much free time does the processor have, in your application?

In this part of the code I actually have plenty of time, do I could digital read each pin and add up the results, it just seemed so ham-fisted. The tips posted here about Port and bit manipulation seems to be getting me into a more efficient ball park...

June 11, 2016, 5:07pm 9

One way of doing it

struct DATARAW {   byte b0: 1;   byte b1: 1;   byte b2: 1;   byte b3: 1;   byte b4: 1;   byte b5: 1;   byte b6: 1;   byte b7: 1; }; union DATA {   DATARAW rawdata;   byte value; }; DATA data; void setup() {   ...   ... } void loop() {   data.rawdata.b0 = digitalRead(pin0);   ...   ...   data.rawdata.b7 = digitalRead(pin7);   Serial.println(data.value); }

Read up on structs, bitfields and unions :smiley:

The above approach does not require bit-shifting (which would be the alternative).

Code not tested.

Topic Replies Views Activity
Convert digital inputs to byte Programming 11 10691 May 5, 2021
Array from digital reads of 16 inputs General Guidance 10 579 June 21, 2023
4 bit variable to 4 different pins Programming 22 391 August 12, 2024
Is There An Easier Way Programming 22 185 February 6, 2025
how to covnert data from 8 pins to one number Programming 7 940 May 5, 2021
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino Write Byte To Pins