See the Number Systems page for an overview of binary numbers.
Part 1 – Observe a 4-bit binary counting sequence on four LED’s.
Build the following circuit, connecting the “DIGITAL” pins to the corresponding microcontroller pins.
int x;
void setup()
{
// make pins 4-7 outputs to drive the led’s
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
// initialize our variable to 0
x = 0;
}
void loop()
{
// call our custom function to write the value in x to pins 4-7
SetBits(x);
// short delay
delay(1000);
// increment our variable
x = x + 1;
// keep x between 0 and 15
if(x == 16)
x = 0;
}
void SetBits(int val)
{
// if the first bit a 1 write a high, otherwise write a low
if(val&1)
digitalWrite(4,HIGH);
else
digitalWrite(4,LOW);
// if the second bit a 1 write a high, otherwise write a low
if(val&2)
digitalWrite(5,HIGH);
else
digitalWrite(5,LOW);
// if the third bit a 1 write a high, otherwise write a low
if(val&4)
digitalWrite(6,HIGH);
else
digitalWrite(6,LOW);
// if the fourth bit a 1 write a high, otherwise write a low
if(val&8)
digitalWrite(7,HIGH);
else
digitalWrite(7,LOW);
}
.
Part 2 – Byte Writing
Modify the code to write a byte value to the port all at once, instead of a bit at a time.
int x;
void setup()
{
// make pins 4-7 outputs to drive the led’s
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
// initialize our variable to 0
x = 0;
}
void loop()
{
// call our custom function to write the value in x to pins 4-7
PortWrite(x);
// short delay
delay(1000);
// increment our variable
x = x + 1;
// keep x between 0 and 15
if(x == 16)
x = 0;
}
void PortWrite(int val)
{
// shift val to the left 4 bits to put it on pins 4-7 and write it to the port
PORTD = val<<4;
}
.
Part 3 – Push button switch
Add the following circuit and code to allow the microcontroller to monitor a switch. Based on the following code the microcontroller will count up one each time the switch is pressed.
int x;
void setup()
{
// make pins 4-7 outputs to drive the led’s
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
// make pin 8 an input to monitor the switch
pinMode(8,INPUT);
// writing a high to an input pin adds an internal pull up resistor
digitalWrite(8,HIGH);
// initialize our variable to 0
x = 0;
}
void loop()
{
// call our custom function to write the value in x to pins 4-7
PortWrite(x);
// wait for the key to be pressed and released
WaitForKeypress();
// increment our variable
x = x + 1;
// keep x between 0 and 15
if(x == 16)
x = 0;
}
void PortWrite(int val)
{
// shift val to the left 4 bits to put it on pins 4-7 and write it to the port
PORTD = val<<4;
}
void WaitForKeypress()
{
// wait for the key input to go high indicating the key is not pressed
while(digitalRead(8) != 1);
// wait for the key to go low indicating the key is pressed
while(digitalRead(8) != 0);
// delay for debounce
delay(20);
// wait for the key input to go high indicating the key is not pressed
while(digitalRead(8) != 1);
}

