You are on page 1of 8

Programming, Communications & Interfacing.

Institute of Technology, Sligo


Ian Craig
INTRODUCTION
Computers are based on binary (1
and 0) and its relative
BINARY & HEXADECIMAL
hexadecimal. We will build a
circuit to create a binary signal,
Laboratory 3
and then modify it to represent a
nibble (4 bits) and a byte (8 bits).

Binary is base 2 (it has 2 digits),


hexadecimal has 16 digits. There
are 4 binary bits in one
hexadecimal digit.
OBJECTIVES
1. Create a simple
switch/LED circuit to
read in 1 binary bit via
the serial port
2. Modify the circuit to
read and evaluate 4 bit
combinations.
3. Modify the circuit to
read and evaluate 8 bit
combinations.
WHAT YOU WILL NEED
 Arduino Uno starter kit
(K000007)
 The Arduino Projects
Book (This is part of the
above kit)
 A PC with connection to
the internet
 Arduino Design Software
(IDE)

Revision 00 PCI Page 1 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig

THE SETUP
For this you will need switches, LEDs, wires and 330Ω resistors.

TASK 1 – SINGLE BIT SWITCH/LED


1. Build the circuit shown in Error: Reference source not found

FIGURE 1 - 1 BIT CIRCUIT

2. The finished circuit should look like Figure 2

FIGURE 2 - TASK 1 CIRCUIT BUILD

Revision 00 PCI Page 2 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig

3. Open up the Arduino IDE and cut and paste the following code

/*
* PCI Lab
* 1 bit
* Will read the state of one switch on pin 2
* Author:
* Date :
*/
// digital pin 2 has a pushbutton attached to it
int pushButton = 2;

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1000); // delay in between reads for stability
}

4. Compile it successfully and upload it to the Arduino.


5. Open the Tools->Serial Monitor on the Arduino IDE on the computer. You should see the output as 1
or 0 depending if you hold down the switch or not. The LED should also light if the switch is pressed
down.

Revision 00 PCI Page 3 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig

TASK 2 – A 4 BIT BINARY TO HEX CONVERTER


1. Add three more LEDs/switches/resistors to the breadboard to make a 4 bit register.
2. The circuit diagram is as follows:

FIGURE 3 - 4 BIT REGISTER

3. A picture of the build circuit is shown in Figure 4

Revision 00 PCI Page 4 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig

FIGURE 4 - 4 BIT REGISTER EXAMPLE

4. Your code should look like:

/*
* Lab 3
* Program to read the state of 4 buttons (a half byte) and print out the
* Binary values, decimal and Hex values.
* Author:
* Date :
*/
// We will use the following pins
int pinButton1 = 2;
int pinButton2 = 3;
int pinButton3 = 4;
int pinButton4 = 5;

// These variables hold the read state of each button


// Button1 is the most significant bit, button4 the least significant.
int stateButton1 = 0;
int stateButton2 = 0;
int stateButton3 = 0;
int stateButton4 = 0;

// Decimal value
int decValue = 0;

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make all the pushbutton's pin as inputs:
pinMode(pinButton1, INPUT);
pinMode(pinButton2, INPUT);

Revision 00 PCI Page 5 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig
pinMode(pinButton3, INPUT);
pinMode(pinButton4, INPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input pins together:
// these will be high or low (1 or 0)
stateButton1 = digitalRead(pinButton1);
stateButton2 = digitalRead(pinButton2);
stateButton3 = digitalRead(pinButton3);
stateButton4 = digitalRead(pinButton4);

// convert the binary to decimal


// Button1 is 2^3 = 8
// Button2 us 2^2 = 4, etc
//
decValue = (stateButton1 * 8) +
(stateButton2 * 4) +
(stateButton3 * 2) +
(stateButton4 * 1);

// print out the state of the buttons


Serial.print(stateButton1);
Serial.print(stateButton2);
Serial.print(stateButton3);
Serial.print(stateButton4);
Serial.println("");

Serial.print("The decimal value = ");


Serial.println(decValue);
Serial.print("The hexadecimal value = ");
Serial.println(decValue, HEX);

delay(1000); // delay in between reads


}

5. Compile this program and upload the code to the Arduino.


6. On your computer, click on the Arduino IDE and choose Tools->Serial Monitor
7. You will see the output for the serial port like Figure 5- Serial Port printoutFigure 5. In this example,
buttons 1 and 4 are being pressed so the value of 1001 binary = 9 decimal.

FIGURE 5- SERIAL PORT PRINTOUT

Revision 00 PCI Page 6 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig

Revision 00 PCI Page 7 of 8


Programming, Communications & Interfacing.
Institute of Technology, Sligo
Ian Craig

TASK 3 - YOUR TASK

 Modify the program in part 2 so you implement an 8 bit register. This will
represent a byte of information. I.e. 0 to 255 decimal, 00000000 to 11111111
binary and 00 to FF hex
 When you press any combination of the 8 buttons, you should display the
binary value, the decimal value and the hexadecimal value on the serial
monitor. E.g. press 10101010 which is 170 decimal and AA hex.
 To do this you will need to:
o Add 4 more LEDs/switches, etc. to the circuit
o Modify the code to handle 8 switches/pins
o Calculate the value of decValue (between 0 and 255)
 Upload (cut and paste) your completed code in lab 3 assignment in
Moodle.
 Take a picture of your Arduino circuit (similar to Figure 4 ) and upload it
in moodle in topic 3

Revision 00 PCI Page 8 of 8

You might also like