Sunday, 19 October 2014

Light Sensing Using Arduino

LDR(Light Dependent Resistor) is the component that is used to sense the light. Its resistance varies with the intensity of light. The resistance of LDR ranges from 5K ohm in bright to 20M ohm in the dark.


LDR-2-2-1-14
The arduino is used simply to display the value of light sensed using ANALOGREAD command. As LDR is a variable resistor, to avoid shorting between +5v and Gnd, a voltage divider is used.

Circuit:LDR1-1

Programming the arduino:

int LDR=A0;
int x;
int sense=100;
int led=8;

void setup()
{
  Serial.begin(9600);
 pinMode(led, OUTPUT);
}

void loop()
{
  
  x=analogRead(LDR);
  Serial.print(x);
  if(x<sense)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  } 
  Serial.print("   ");
  delay(500);
  
}

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.

Friday, 27 June 2014

Colour Mixing of RGB

I was unable to get the diffused common anode RGB led. So I put red, blue and green LEDs together using a tape. Now I could notice the colour mixing of intersection.

In the hex code of colours, each 2 characters represent the value of red, green and blue sequentially. To convert the hex code of a colour into its number form, 0x  is prefixed to the hex value.

For example the code for Deep pink is #FF1493 . So its red value is FF, green is 14, blue is 93.
So in my program, setcolour(0xFF, 0x93, 0x14) gives me deep pink

Breadboard Layout:


Code:

int red=11;
int blue=10;
int green=9;
void setup()
{
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
}

void loop()
{
  setcolour(255,0,0);//red
  delay(2000);
  setcolour(0,255,0);//blue
  delay(2000);
  setcolour(0,0,255);//green
  delay(2000);
  // #FFFF66: yellow
  setcolour(0xFF, 0xFF, 0x66);
    delay(4000);
  setcolour(0xFF, 0x93, 0x14);//pink
     delay(4000);
  //spring green #00FA9A
  setcolour(0x00,0x9A, 0xFA);
  delay(4000);
}

void setcolour(int red_value, int blue_value, int green_value)
{
  analogWrite(red, red_value);
  analogWrite(blue, blue_value);
  analogWrite(green, green_value);
}

Fading using Pulse width modification

 Pulse Width Modification is a power controlling technique where we get analog results from digital means. In this we modify the on-off pattern of a digital clock to vary the analog values.
As we mix the colour paints on the palette, we can mix the colours of LEDs using PWM. This can be done using the command analogWrite.
analogWrite() takes the value from 0-255.
255=100% duty cycle;    127=50% duty cycle;

Fading of LED:

Circuit layout:



Code:


int led=11; int brightness=255; int fade=5;
void setup()
{
  pinMode(led, OUTPUT);
}
void loop()
{
 analogWrite(led, brightness);
 brightness=brightness-fade;
 if(brightness==255||brightness==0) //reversing effect
 !fade;
 delay(100);
}


Sunday, 22 June 2014

My First Project-BLINK

Being an ECE student having not gone to any intern, I thought of doing some projects while on my vacation. I found arduino as a good platform to start with. I downloaded the arduino IDE from this link   http://arduino.cc/en/Main/Software . After installing the software, we need to initialize the serial port to which arduino is connected..Here I begin :D :D

So What is Arduino?

From the Arduino.cc homepage:
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

Blinking 5 LEDs in a row for a second each

This is the most basic arduino project. You can use the inbuilt L-LED connected to 13th pin of the arduino board or connect external led.

Arduino code consists of two parts basically:
1. setup() : it runs when the arduino is initialized(reset)
2. loop(): it runs till arduino is off.

  • pinMode is the command to assign a pin of arduino board to either input or output mode
  • digitalWrite is used to set a pin to digital High (on) or low (off)
  • delay( ) takes the value in milliseconds for delay

Breadboard Layout:


Code:


/* My First Program */

int pin2=2;
int pin3=3;
int pin4=4;
int pin5=5;
int pin6=6;

void setup()
{
  pinMode(pin2, OUTPUT);
   pinMode(pin3, OUTPUT);
    pinMode(pin4, OUTPUT);
     pinMode(pin5, OUTPUT);
      pinMode(pin6, OUTPUT);

}
void loop()
{
   digitalWrite(pin2, HIGH);
  delay(1000);
  digitalWrite(pin2, LOW);
  delay(1000);
     digitalWrite(pin3, HIGH);
  delay(1000);
  digitalWrite(pin3, LOW);
  delay(1000);
     digitalWrite(pin4, HIGH);
  delay(1000);
  digitalWrite(pin4, LOW);
  delay(1000);
     digitalWrite(pin5, HIGH);
  delay(1000);
  digitalWrite(pin5, LOW);
  delay(1000);
     digitalWrite(pin6, HIGH);
  delay(1000);
  digitalWrite(pin6, LOW);
  delay(1000);
}

Output: