Sunday, 21 July 2013

Digital Outputs - What, Where & How? - Working with the 3 on-board LEDs

Digital Outputs

Digital Outputs are used to Produce On / Off type of control. Digital Signals can be either 'HIGH' (ON) or 'LOW'(OFF). Using Digital Outputs, we can generate HIGH / LOW signals and control the devices connected to it. We can control LEDs, Relays, Etc.

All the I/O pins on the Induino R3 (& All Arduino Boards) can be used for Producing Digital Outputs. When working with Digital Signals pins A0-A5(Analog In) are referred to as 14-19 for convenience sake. So in total there are 20 pins (0 to 19) that can work with Digital Signals.

On the Induino R3 Board, we have 3 LEDs connected to pins 11, 12 & 13.  We can use these to experiment with Digital Outputs.

Lets do a Blink Program to Blink the RED LED on Pin 11. We would need to use 3 functions for this
  1. pinMode(pin number, mode) => The pinMode() function is used to initialise a given pin either as OUTPUT or INPUT or INPUT_PULLUP (used in the next tutorial). We require this function to be called only once during the initial power on state of the board, so we will call this function from within out setup() function. 
  2. digitalWrite(pin number, state) => The digitalWrite() function is used to write a digital signal (HIGH or LOW) on the given pin number. In other words it helps us control a given pin.
  3. delay(milliseconds) => The delay() function pauses the program for the amount of time give in milliseconds.
Heres our Blink program using these functions


/*Induino R3 User Guide - Program 1.0 -  To Blink LED on Pin 11 */

void setup() // Executed only once at Power On / Reset
{

pinMode(11,OUTPUT); // Initialises Pin 11 as an OUTPUT Pin

}

void loop() // keeps on running infinitely till the board is powered off
{

digitalWrite(11,HIGH); // Turns Pin 11 ON
delay(1000); // Waits for 1 second (1000 milliseconds = 1 second)
digitalWrite(11,LOW);// Turns Pin 11 OFF
delay(1000); // Waits for 1 second (1000 milliseconds = 1 second)

}


Playing Around with the digitalWrite() function

The digitalWrite() function can be modifed to use '1' / '0' instead of HIGH and LOW or we can even use a variable. Incase of a variable, if the value of the variable is zero it is considered to be a LOW signal and if it is a non-zero it is considered as a HIGH signal.

So First Try this, in the above program replace as follows

digitalWrite(11,HIGH); // Turns Pin 11 ON
digitalWrite(11,1; // Turns Pin 11 ON

digitalWrite(11,LOW);// Turns Pin 11 OFF
digitalWrite(11,0);// Turns Pin 11 OFF

Next, Lets experiment with some variables

The pinMode() function can also take a variable as a parameter for pin number.

NOT Operator => The NOT Operator is represented by ! symbol. The NOT (!) operator returns a 0 when given a non-zero value and 1 when given a zero.

Lets rewrite the program using variables and a NOT(!) operator.


/* Induino R3 User Guide - Program 1.1 -  To Blink LED on Pin 11 using a Variable with digitalWrite() function*/

int led_pin = 11; // using a variable to refer to the led pin
int i = 0; // i is a global variable as it is declared outside the setup() and loop() functions. It gets initialised at Power On / Reset

void setup() // Executed only once at Power On / Reset
{

pinMode(led_pin,OUTPUT); // Initialises Pin 11 as an OUTPUT Pin

}

void loop() // keeps on running infinitely till the board is powered off
{

i = !i; /* The NOT (!) operator returns a 0 when given a non-zero value and 1 when given a zero. So here the value of i keeps toggling between 0 and 1 */
digitalWrite(led_pin,i); // Turns Pin 11 ON
delay(1000); // Waits for 1 second (1000 milliseconds = 1 second)

}

The Binary Counter


A Binary Counter is an interesting project for the beginner to get started right after blinking an LED. A Binary counter is a simple counter that displays the current counter value in binary format. we have 3 On-board LEDs connected to Pins 11,12 &13 on the Induino R3, we can use these to build a 3-bit binary counter that will automatically increment every second and reset once it reaches 7.

We can assume the LED connected to PIN 11 to be the LSB [Least Significant Bit] and the LED connected to PIN 13 as the MSB [Most Significant Bit] . We can have a counter variable that we can increment till 7 and reset to 0 on reaching 7.

Things to Note
  • The pinMode() & digitalWrite() functions can take only 1 pin number as a parameter, so we would require 3 pinMode() functions and 3 digitalWrite() functions in our program.


Now Lets take a look at the logic

Heres how a Binary counter will work, for each value of the variable Counter(0 - 7), the same shall be displayed in the Binary Format.
     
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
0 0 0
...... and on

If you notice, the LSB is toggling every cycle, the Middle Bit is toggling every 2 cycles and the MSB is toggling every cycle, To achieve this effect, we can use the MOD operator (%)

Counter % 2 will produce an alternating output of '1' & '0' that changes every cycle

(Counter/2) % 2 will produce an alternating output of '1' & '0' that changes every 2 cycles(whatever you divide by!)

(Counter/4) % 2 will produce an alternating output of '1' & '0' that changes every 4 cycles

Heres the code for this...

 /*   Induino R3 User Guide - Program 1.2 - Binary Counter
 This sketch automatically increases a 3 bit number every second  
 and shows the output on 3 LEDs   
 */  
 void setup()  
 {  
  pinMode(11,OUTPUT);   // declare LED pins as output pins  
  pinMode(12,OUTPUT);  
  pinMode(13,OUTPUT);  
 }  
 void loop()  
 {  
  for(int i=0;i<8;i++)  // increment automatically from 0 to 7 , i is the counter variable
  {  
   int a=i%2;      // calculate LSB   
   int b=i/2 %2;     // calculate middle bit  
   int c=i/4 %2;     // calculate MSB   
   digitalWrite(11,a);  // write MSB  
   digitalWrite(12,b);  // write middle bit  
   digitalWrite(13,c);  // write LSB  
   delay(1000);     // wait for a second  
  }  
 }  

Thats It For This Part! Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in

Visit www.simplelabs.co.in for more interesting products

Back to List of Contents

1 comment:


  1. Heya i'm for the primary time here. I came across this board and I to find It really helpful & it helped me out a lot. I am hoping to give something again and help others such as you aided me. capital one login

    ReplyDelete