Posts Tagged ‘breadboard’

Blinking Binary Bits and Bytes…

June 9, 2009

Looking At The Numbers.

Learning to make an LED flash is a great way to begin programming your Arduino, so take what you’ve learned with the LEDBlink program and add more LEDs. How many should we add, 3, 4, 7, 15 or more? Is there a special number of LEDs to choose? The Arduino Duemilanove has a total of 14 Digital pins and 6 Analog pins that we could use for a maximum of 20 individually controlled pins.

Lets take a look at how computers understand numbers, incidentally microcontrollers like those used by your Arduino understand numbers the same way. Arduino uses the Atmel Corporation’s 8-Bit family of microcontroller devices. Atmel manufactures a series of 8 or 16 bit devices. Most modern personal computers use either 32 or 64 bit hardware. This is beginning to suggest that a multiple of eight is important.

Maybe you’ve heard someone say that computers work with binary numbers which are a bunch of ones and zeros. Individually the “1” or “0” values are stored in binary digits that are called bits. The word bits is actually a contraction of the two words “binary” and “digits“. Our computers and microcontrollers both work with these ones and zeros in multiples of eight. Collectively these eight binary digits are known as a byte, so there are generally 8 bits per byte.

Bytes Are Important.

A Byte represents the smallest segment of memory containing data that an Arduino will read or write. A byte of data can save a positive whole number with a value ranging from 0 to 255. Lets create a program to show how a computer can count using binary numbers. Each LED will represent a single byte in an 8-Bit number.

Wiring the Breadboard

The breadboard gets wired up like the example from the “Learning the C Language with Arduino” article. This example adds more LEDs for a total of eight. The LEDs are attached to Digital pins 3 through 10. A total of eight LEDs and eight resistors are needed for this project.

Why are we starting with Digital Pin3? First, we are using the digital pins because they work like switches, either turning on (HIGH) or off (LOW). There is no significance to starting with pin 3 when any of the other digital pins would work just as well. Pins 0 and 1 carry a dual role. They act like other digital pins but also may handle serial communication’s TX and RX (transmit and receive) features.

Breadboard Layout 8 LEDs

Breadboard Layout 8 LEDs

Create a new program named:binaryCount

Type in the following code, typing the comments (shown in green) are optional. You could copy and paste into the program editor but typing your own code helps you learn the programming language too.

/*— Binary Counting with 8 bits —*/
//Associate LEDs with an Arduino Digital pin.

int led0Pin = 3;
int led1Pin = 4;
int led2Pin = 5;
int led3Pin = 6;
int led4Pin = 7;
int led5Pin = 8;
int led6Pin = 9;
int led7Pin = 10;

void setup()
//Set up each of the pins for output only.
pinMode(led0Pin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);
pinMode(led7Pin, OUTPUT);

void loop()
byte iVal; //we’ll define this variable for use in the program.
//A byte is an 8 bit variable.
// begin counting up from 0 to 255

for(iVal=0; iVal<255; iVal++) // loop through each of the values { // Light up LED if its corresponding byte is equal to binary va1ue. digitalWrite(led0Pin, (iVal & B1)); // -------X Decimal value 1 digitalWrite(led1Pin, (iVal & B10)); // ------X- Decimal value 2 digitalWrite(led2Pin, (iVal & B100)); // -----X-- Decimal value 4 digitalWrite(led3Pin, (iVal & B1000)); // ----X--- Decimal value 8 digitalWrite(led4Pin, (iVal & B10000)); // ---X---- Decimal value 16 digitalWrite(led5Pin, (iVal & B100000)); // --X----- Decimal value 32 digitalWrite(led6Pin, (iVal & B1000000)); // -X------ Decimal value 64 digitalWrite(led7Pin, (iVal & B10000000)); // X------- Decimal value 128 delay(1000); } digitalWrite(led0Pin, (iVal & B1)); delay(2000); } [/sourcecode]

Source Code Analysis

Line 4

Why is the first variable, led0Pin named with 0 instead of 1?

The short answer is the C Language starts counting beginning with the number 0. In the led0Pin through led7Pin variables the numeric character is irrelevant except to differentiate between the names. A later article will describe using variable arrays where this becomes important. This way of naming variables was done for some consistency with future examples.

Line 32

The ” for” statement is part of the C language’s control structures.

for(iVal=0; iVal<255; iVal++) // loop through each of the values

There are three parts to the for control loop:

  • Initialization – “iVal=0;” This assigns the value of 0 to the iVal variable. You need to end this portion with a semicolon.
  • Condition – “iVal<255;” This checks if the variable iVal is less than 255. While this condition is true the loop continues again. You need to end this portion with a semicolon.
  • Increment – “iVal++”  After processing the statements within the for loop the variable iVal is incremented by one. This portion does not use a semicolon.

In the iVal++ increment portion the plus plus characters “++“, this is the same as assigning iVal = iVal + 1 or adding one to the iVal variable each time the for loop finished.

Since the for() statement is part of the control structure you don’t put a semicolon at the end of this statement.

Lines 34 through 41

digitalWrite(led0Pin, (iVal &        B1)); // ——-X Decimal value 1

LED Bit Values

LED Bit Values

Bit values

As described above, we are using LEDs to represent each of the eight bits in a one byte value. Each bit signifies a value for that column. If all of the LEDs are off then the number it represents is zero (0). If only the right-most LED in the column labeled “1” is lit up then the byte of data is equal to the number 1. If the left-most column labeled “128” is the only one lit up then the byte of data is equal to the number 128. If all LEDs are lit up then the byte of data is equal to the number 255.

128 + 64 + 32 + 16 + 8 + 4 + 2 + `1 = 255

Any combinations of the eight LEDs that are turned ON or OFF represent a number from 0 to 255. Similarly, we can create a binary representation of the number by using a Bit Formatter. This is a value using the upper case letter B with a combination of only ones (1) and zeros (0) as shown below.

  • – – – – – – – X Decimal value 1  Bit Format:    B1
  • – – – – – – X – Decimal value 2  Bit Format:    B10
  • – – – – – X – – Decimal value 4  Bit Format:    B100
  • – – – – X – – – Decimal value 8  Bit Format:    B1000
  • – – – X – – – – Decimal value 16  Bit Format:  B10000
  • – – X – – – – – Decimal value 32  Bit Format:   B100000
  • -X – – – – – –  Decimal value 64  Bit Format:    B1000000
  • X – – – – – – – Decimal value 128  Bit Format: B10000000

The Bitwise AND “&” Operator

(iVal & B1)

If we have a the variable iVal which contains a value ranging from 0 to 255, how can we use that value to turn the pattern of LEDs on or off that the number represents? Because we’re only checking the iVal value against a single bit each time, the statement (iVal & B1) returns a TRUE (1) or FALSE (0) answer. The ampersand is a bitwise AND operator. In the example if the right most bit in the variable iVal is 1 AND the right most bit as specified by B1 are both 1, then the bitwise AND is 1 if they both aren’t set to 1 then the bitwise AND is evaluated as 0.

(c) 2009 – Vince Thompson

Learning the C Language with Arduino

June 4, 2009

Wiring the Breadboard

In this example we’re using a single LED on the breadboard wired to the Arduino. As shown in the example, attach a ground wire (black) to the Gnd location on the bottom of the Arduino. Connect the wire to a ground rail on the breadboard. Using additional black wires jump to the bottom ground rail on the breadboard to complete a circuit adjacent to the LED position on the breadboard.

Place the LED in the breadboard and connect the black ground wire between the ground rail and the cathode lead (-) on the LED. Place a 150 to 220 ohm resistor in the breadboard. Attach another wire from one end of the resistor to the anode lead (+) of the LED. Finally, attach a wire between the remaining end of the resistor and Digital Pin 3 on the Arduino to compelete the circuit. This will allow us to write a program that controls flashing the LED.

Breadboard Wiring LED Circuit

Breadboard Wiring LED Circuit

Programming the Arduino

Create a new program named: LEDBlink

Type in the following code, typing the comments (shown in green) are optional. You could copy and paste into the program editor but typing your own code helps you learn the programming language too.

LEDBlink Arduino Program

/*--- Blink an LED  ---*/
//Associate LEDs with an Arduino Digital pin.
int ledPin = 3;  //We're using Digital Pin 3 on the Arduino.</code>

void setup()
   pinMode(ledPin, OUTPUT);   //Set up Arduino pin for output only.

void loop()
   //The HIGH and LOW values set voltage to 5 volts when HIGH and 0 volts LOW.
   digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.
   delay(1000);  //Get the microcontroller to wait for one second.
   digitalWrite(ledPin, LOW);  //Setting the pin to LOW turns the LED off.
   delay(1000);  //Wait another second with the LED turned off.

C Language Programming

As described in the related article “Introduction to Programming the Arduino“, the programming language used for the Arduino is based primarily on the C language.

Language Reference

The Arduino’s language features used in this program example are described in the Language Reference section of the Arduino web site.


In the following program statement we are doing three things as we declare a user defined variable. First we are identifying the data type as being an integer number int”. Second, the variable name ledPin was made up for this program. Third we are assigned a value to this variable. In this case we assigned the value 3 to the variable ledPin.

int ledPin = 13; //We’re using Digital Pin 3 on the Arduino.

Syntax, Punctuation Usage

; semicolons

{} curly braces


One of the differences between programming languages like C and Visual Basic is the use of semicolons to end a statement.

When not to use semicolons.

  • They are ignored if used in comments.
  • They are not used in a function definition like setup()
  • They are not used on the curly braces { }

When to use semicolons.

Semicolons are used to end program statements. In the LEDBlink program they are used to end the variable declaration, ledPin, and to end program statements like pinMode, digitalWrite, and delay functions.

Curly Braces

In the LEDBlink program, the braces are used to completely surround the collection of statements used to make up the setup() function. They are also used separately to surround the loop() function’s program statements.

Standard Arduino Functions

This program only uses three different function calls.

  • pinMode()
  • digitalWrite()
  • delay()


pinMode(ledPin, OUTPUT); //Set up Arduino pin for output only.

In our example project, we’re using one of the Arduino’s digital pins to make an LED turn on and off. These pins are able to operate in an input mode as if to read a switch to see if it is on or off. The pins can operate in an output mode for our LED. As shown in the line of code above we are setting the pin to operate in the output mode.


digitalWrite(ledPin, HIGH); //Setting a digital pin HIGH turns on the LED.

The digitalWrite function does the actual work for us by turning the LED on or off. By specifying a High value it causes the output voltage to go high, in this case we’re using 5 volts to light the LED. Causing digitalWrite to go HIGH turns the pin on with 5 volts. When we want to turn the LED off we use digitalWrite and LOW to cause the pin to go to 0 volts.


delay(1000); //Get the microcontroller to wait for one second.

The article “Microcontrollers as Time Keepers” provides some additional information about the time intervals used within the Arduino’s capabilities. We will be using millseconds (one thousandth of a second) with this program. The delay() function causes the program to wait the specified number of milliseconds, in the following code statement it waits 1000 milliseconds for a total of one second.

Getting it Right the First Time

Hopefully you typed everything correctly getting your punctuation right with the right spelling, upper case and lower case letters as required. Selecting the Sketch -> Verify/Compile will soon let you know if something is wrong. If so, what do you do to correct the problems? Next, we’ll take a look at some possible error conditions and the error messages you might see when writing your own programs.

Check out the article “Correcting Arduino Compiler Errors“.

(c) 2009 – Vince Thompson