Friday, 11 July 2014

DIGITAL INPUTS

In this project, we will try to control the LED using two push buttons by reading the current status of the button.

THE PUSH BUTTON:

Fig: PUSH BUTTON
The working of a push button is clearly understood by the picture beside. Pins A, C and B,D are internally connected. When the button is pressed, all the four pins get connected.


BREADBOARD LAYOUT:
Fig: Breadboard layout
Created using Fritzing


Pressing the top button will turn on LED and pressing the bottom button will turn it off.

NEW CODING TERMS:
  • INPUT_PULLUP : This is the mode of pin (similar to OUTPUT mode). It simply means that the pin is in INPUT mode and initialized to HIGH
  • digitalRead(pinnum) : reads the digital value (ON/OFF) of the pin "pinnum"

CODE:

int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  
}

void loop() 
{
  if (digitalRead(buttonApin) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }
  if (digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, LOW);
  }
}









Wednesday, 2 July 2014

Controlling Arduino through the SERIAL MONITOR

This is a continuation to my previous project of Using 74HC595. In this project we interact with the arduino through the serial monitor.

Link To My Previous Project

THE SERIAL MONITOR:
The Serial Monitor is basically a tether between the computer and the arduino. It is marked with the red circle below.

New Keywords in the code:
  • Serial.begin(9600): It starts serial communication with the arduino at 9600. This 9600 is the baud rate which basically defines the speed of communication.
  • Serial.print("text to be printed on the serial monitor"): It is like the cout command of c++ 
  • Serial.println("txt followed by new line"): It prints the text and moves to a new line.
  • Serial.available() : It checks if the serial monitor is on (available)
CODE:



int DS_pin=8;
int SH_pin=10;
int ST_pin=9;

void setup()
{
  pinMode(DS_pin,OUTPUT);
  pinMode(ST_pin,OUTPUT);
  pinMode(SH_pin,OUTPUT);
  Serial.begin(9600);
  Serial.println("Enter '0' to '7' or 'x' to clear");
}

byte registers;

void loop()
{
  char ch;
  int led;
  if(Serial.available() > 0)
  {  
    ch= Serial.read();
    if(ch >='0' && ch <='7')
    {
      led= ch- '0';
      bitSet(registers, led);
      Serial.print("turned on led");
     Serial.println( ch );
      writereg();
    }
    if(ch=='x')
    {
      registers=0;
      writereg();
      Serial.println("cleared");
    }
  }
}

void writereg()
{
  digitalWrite(ST_pin, LOW);
  shiftOut(DS_pin, SH_pin, LSBFIRST, registers);
  digitalWrite(ST_pin, HIGH);
}
------------------------------------------------------------------------------------------------------------
Remarks:

This is a self explanatory code. If the serial monitor is enabled, it prompts to enter a value from 0 to 7 or x. This is inputted as character. If the value is '0' to '7', then a variable "led" of integer datatype is assigned as the difference of ASCII  values of entered character and that of '0' and will turn on that led using bitSet( ).
If 'x' is entered , "registers" byte will be assigned to zero and the shift register is updated to "00000000". Thus, all LEDs will be off.



Using Shift Registers (74HC595)

This is one of the trickiest but most useful arduino project. Arduino pins are limited. When running out of pins, this method can be used to expand the outputs. Using 74HC595 IC,  I am able to control 8 LEDs using 3 pins.


About 74HC595:

74HC595 is a parrallel serial shift register. It has 16 pins with 8 memory locations, output enable(active low), Storage clock pin(latch pin), memory reset(active low), Shift Clock pin(clock pin), Serial Data pin, Vcc and ground.
The Pinnig Diagram is as shown below:


BreadBoard Layout:
To enable the output, pin 13 is connected to ground. Not to clear the memory, 10th pin is connected to Vcc. Pins 1-7, 15 are connected to LEDs via resistors. Pins 11 , 12 and 14 are connected to the arduino . 



Building virtual circuits : http://fritzing.org

New terms in code:
  • byte : it is datatype that stores value of 8 bits
  • bitSet(x, n): assigns '1' to nth bit of variable x
  • bitClear(x, n):assigns '0' to nth bit of variable x 
  • shiftOut() : (This thing confused me a lot!!) It shifts out a byte of data either starting from leftmost   bit(MSBFIRST) or rightmost bit(LSBFIRST) 
                            Syntax: shiftOut (pin1,  pin2, order,  variable)
        • <int> pin1: data pin to which bit in the byte variable is to be shifted
        • <int> pin2: clock pin to toggle
        • order: MSBFIRST or LSBFIRST
        • <byte> variable: bits which are to be shifted to the data pin

Code:



int DS_pin=8;
int SH_pin=10;
int ST_pin=9;

void setup()
{
  pinMode(DS_pin,OUTPUT);
  pinMode(ST_pin,OUTPUT);
  pinMode(SH_pin,OUTPUT);
}

byte registers;

void loop()
  registers=0;
  writereg();
  for(int i=0; i<8; i++)
  {
   bitSet(registers, i);
    writereg();
    delay(500);
  }
   for(int i=7; i>=0; i--)
  {
   bitClear(registers, i);
    writereg();
    delay(500);
  }

}

void writereg()
{
   digitalWrite(ST_pin,LOW);
  shiftOut(DS_pin, SH_pin, LSBFIRST, registers);
  digitalWrite(ST_pin, HIGH);
}

Explanation to the code:

The variable "registers" is assigned the datatype byte and is intialized to "00000000" within the loop. In writereg()  function, a transition from low state to high state of Latch pin(ST_pin)  is created in which the data is shifted. Shifting out the value of "registers" to the data pin (DS_pin) bit by bit is done for every clock transition (SH_pin) while this is latched onto the register for every transition in latch pin. 
Refer the function table in the 74HC595 Datasheet.
There are two for loops within the setup(), one for glowing the LEDs and other for turning it off.
In the first for loop, bitSet is used for turning on an LED and shift register is updated by writereg() .The second for loop is just the reverse of the first one. It uses bitClear instead of bitSet for switching off the LEDs.