Thursday, 31 December 2015

The Story Of An Arduino

The Arduino is a 8 bit micro-controller board. This means , that it can send and recieve data in 8 bits only. It runs on an IC made by a company called Atmel. You will see it inscribed on top of the IC.
Arduino UNO usually runs on Atmel 168. This link will take to the datasheet of the IC:

www.atmel.com/devices/atmega168.aspx

These ICs employ the AVR architecture which was conceived by two students at the Norwegian Institute of Technology. This architecture ( meaning the layout of circuit components on the IC) , took the micro-controller industry by storm.

Current AVRs offer a wide range of capabilities. These include :
1. Integrated SRAM , EEPROM and Flash memories. This negates the requirement of connecting external memory elements. (The bootloader program , and the one you upload are stored here)
2. Analog to Digital and Digital to Analog converters.
3. USB support and endless other functionalities.
Read more :https://en.wikipedia.org/wiki/Atmel_AVR

The Arduino itself was conceived by Massimo Banzi. The creators of Arduino named it after a bar where they met often.

An interesting interview of Massimo Banzi : 

https://twit.tv/shows/triangulation/episodes/110


If you have ever wondered what the silvery thing below the IC is - it is the clock signal generator. Basically a quartz crystal that oscillates at the frequency of 16MHz. Some circuits are clocked, which means their operation is dependent on a clock signal. The circuit operates only during half the clock cycle. This is helpful for many bit operations.

 The small thing next to the USB connector is the FTDI chip ,  which is the USB to serial adapter. You probably installed an FTDI driver when you powered up the arduino with your computer for the first time. Now you know why.

There are two L.E.Ds on board with the tx and rx marking. tx(transferx) and rx(receivex) light up whenever data is transferred or received by the board respectively. You see them light up when you upload a program to the Arduino using your computer. These two (tx and rx) are available through pins 0 and 1 on the arduino. You will use these when use Xbee radio chips with the arduino.

The ICSP pins are used to program the arduino with a external programmer , if you decide not to use the user friendly IDE built for us.

The reset button needless to say, makes the arduino start from square one.
The Arduino gives you 13 input/ output digital pins. Of these 6 are PMW (pulse width modulation) enabled. There are three ground pins and 6 analog input pins.
There are many variations of the Arduino:

https://www.arduino.cc/en/Main/Boards

The Arduino hardware specifications are openly available, which means you do not need to buy the board. If you have the heart , you can build it from scratch.
Time now to put your creative hats on and make something wonderful. See you around :)

Sunday, 22 November 2015

A Taste of Home Automation


This tutorial will help you setup an entry/exit detector at your doorstep. I paired it up with a relay to control the lights in my room. So , depending upon the number of people in the room , lights are switched on and off. I will break down the tutorial in two parts :

1.Entry/ Exit Detection
We will use two HC-SR04 sensors. These are capable of emitting and detecting ultrasonic sound waves. Here is how one of these looks :

          


The idea is to use them to send sound pulses continuously. We will measure the time required to detect a pulse that has bounced off some object. Using this time and the known average speed of sound in air , we will calculate the approximate distance of the object from the sensor. Now , if a person enters or exits, this value of distance will change. This change thus becomes an indication of if something has passed through the door.  If we use two sensors kept next to each other , we can also detect if that someone has entered or exited , depending upon which sensor's value changes first.

About the sensor : We see it has four pins. Vcc goes to +5V of Arduino. Trig Pin when high produces a sound pulse. Echo pin is high when the reflected pulse falls on the sensor.

2. Controlling The lights

Now that we know , whenever someone has entered or exited the room, we use this information to control lights. You can use a relay or any other mechanism to switch the light on or off. I used a motor to do it. I agree that it is a very inefficient way but given that nobody at home would be happy with me opening switchboards and pulling wires out of it , I took the other road.
Now , whenever no one is in the room , the motor turn to turn the light switch off.
See my earlier posts to learn how to use motors with the arduino.
This is how my setup looked :


WIRING IT UP
  
                        

Created using Fritzing

THE PROGRAM

#define trigPin1 9 // HC-SR04 pins
#define echoPin1 8
#define trigPin2 11#define echoPin2 10
int in1Pin = 6; // Motor Pins
int in2Pin = 7;

int n=0,l=0,m=0;
// n : No. of people in the room
void setup()
{
 Serial.begin (9600);

 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin1, INPUT);
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 pinMode(in1Pin, OUTPUT);
 pinMode(in2Pin, OUTPUT);

}

long ping(int trigPin,int echoPin)// returns distance
{
 long duration, distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 return distance;
}

void loop()
{
 
  long x,xx,y,yy; // variable to store distances
  x=ping(trigPin1,echoPin1);
  xx=ping(trigPin1,echoPin1);
  if((x-xx)>20) // Change this to increace/decrease sensitivity
  {Serial.println("Entered ");
   n++;
   delay(1000);
  }
  else
  {
  y=ping(trigPin2,echoPin2);
  yy=ping(trigPin2,echoPin2);
  if((y-yy)>20)
  {Serial.println("Exited  ");
  n--;
  delay(1000);
  }
  }
  Serial.println();
 
  if (n==-1) n=0;
  if (n==1 && l==0)
  {ON();l++;m++;}
  if(n==0 && m!=0)
  {OFF();m=0;l=0;}
  Serial.println("No. of people in room:  " );
  Serial.print(n);
 
}
void ON() // runs motor to turn on switch
{
  digitalWrite(in2Pin, HIGH);
  digitalWrite(in1Pin, LOW);
  delay(300);
  digitalWrite(in2Pin, LOW);
}
void OFF() // runs motor to turn off switch
{
  digitalWrite(in2Pin, LOW);
  digitalWrite(in1Pin, HIGH);
  delay(300);
  digitalWrite(in1Pin, LOW);
}


-----------------------------------------------------------------------------------------------------------------------------

Monday, 12 October 2015

Making A Line Following Robot

The working of an LFR , is very simple. It has two sensors placed on either side of the line. If the left sensor comes on the black line , the left motor should stop , for it to correct its position . Similarly if the right sensor comes above the black line , the right motor should stop moving.

How is the black line detected
Black and white colours have different optic properties. While black absorbs light of all colours incident on it , white reflects most of these back. So naturally when the sensor is above the white portion , it will receive a higher intensity of light and thus will provide a higher value.
Our task here then is to make a sensor that can convert light intensity , into a voltage reading , which the Arduino can read via its analog input pins.
We will use a photo resistor , which is a device whose resistance to current changes with the incidence of light intensity.

Making the sensor 

The sensor is a basic voltage divider circuit. The photo-resistor is connected in series with a 100 ohm resistor. On end of this combination is connected to +5v and other to ground. The junction is connected to any of the analog pins on the arduino. A resistor is added to this connection to prevent damage to the arduino due to large currents. The junction potential changes when the resistance of the photo resistor changes. This change in potential is measured by the Arduino and is taken advantage of .







Created using Fritzing
Wiring The Motors.

Check my previous post on running motors using the Arduino.
http://crazzycircuits.blogspot.com/2015/07/how-to-run-motor-using-l293d-double-h.html




The Program:

#define M1pin1 9
#define M1pin2 8
#define M2pin1 10
#define M2pin2 11
#define Sensor1 A0
#define Sensor2 A1

int sensor_value1,sensor_value2,diff;

void setup()
{

 Serial.begin (9600);
 pinMode(M1pin1, OUTPUT);
 pinMode(M1pin2, OUTPUT);
 pinMode(M2pin1, OUTPUT);
 pinMode(M2pin2, OUTPUT);

 sensor_value1 = analogRead(Sensor1);
 sensor_value2=analogRead(Sensor2);
 diff= sensor_value2-sensor_value1;  
// We account for this difference to equalise both sensor inputs values for the white portion. This   little trick eliminates the need for calibration.
}

void loop()
{

  sensor_value1 = analogRead(Sensor1)+diff;
  sensor_value2=analogRead(Sensor2);
  if(sensor_value2-sensor_value1>1)  //Sensor values differ when either sensor is on the black   line
     OFF(M2pin1,M2pin2);
  else
     ON(M2pin1,M2pin2);
   
   
 // ______________________
 
  if(sensor_value1-sensor_value2>1)
     OFF(M1pin1,M1pin2);
  else
      ON(M1pin1,M1pin2);
 
 
  //Serial.println(sensor_value1);
  //Serial.println(sensor_value2);
 
}

void ON(int Mpin1,int Mpin2) // runs motor 
{
  digitalWrite(Mpin1, HIGH);
  digitalWrite(Mpin2, LOW);
  
}
void OFF(int Mpin1,int Mpin2) // Stops motor
{
  digitalWrite(Mpin1, LOW);
  digitalWrite(Mpin2, LOW);
}
//End of Program
...................................................................................................................................................................

Sunday, 16 August 2015

Tracker Camera


I will start with : It was a difficult project . Very challenging.
From learning to work with servos, to using the opencv library , to getting my mind around using a raspberry pi , this was one hell of a learning experience.
The camera basically tracks objects based on their unique colour. The camera feeds what it sees into a raspberry pi , which then uses its computing might , to threshold , morph, and the extract features , all in real time. The extracted coordinate of the centroid of the object are then used to position , the servos. This ,trust me, is no easy task. All of this is implemented using the opencv library for image processing. The GPIO library(wiring pi) for raspberry pi helps me interface with the servos.
I spent a lot of time on this project. It has made me fall in love with the raspberry pi.
Post this project , what has mostly changed is my hair pulling threshold ( meaning the failed attempts after which I start to pull my hair). This has definitely increased, for good.
Here , have a look at what came of the madness :


Friday, 24 July 2015

Line follower

I made this over winter. It took me a week to complete this. Making a line follower is pretty simple.
This one has a special something though. I made my own light sensors , unlike those available in the market. It was based on what I had learned in my first semester electrical course class. Also this one need not be calibrated in different light conditions. It does so itself , with a very simple tweak , that just came to me , somehow. It thus does not run on the same piece of program that is flooded over the internet.
I have shared how to make a line follower :
Here is mine , in action.

Thursday, 23 July 2015

How to run a motor using L293D (double H-bridge) and interfacing it with the Arduino


Motor drivers are needed because the pins of a micro-controller cannot source the required current to run a motor.
The arduino can provide a maximum 200mA per pin and can sink a total of 400mA current.

You will damage the micro - controller or pins if you try to draw more current. A motor cannot run on such small current. Thus an amplifier circuit or a relay is needed. To drive the motor in both directions , an H-bridge is needed.

Lo and behold ! Let me present to you L293D. It is a transistor implementation of both an amplifier and an H-bridge , in other words , the solution to our problem.

This is what an h-bridge looks like:

 

This is the pin layout of L293D:




















It can run two motors.
Vss needs to be connected to 5v for either h-bridge to run.
Enable1, if connected to 5v enables the left h- bridge.
Enable2, is for right H- bridge.
Gnd pins go to ground. 
Vs  is the voltage you want to give the motor.


 WIRE IT UP


Created Using Fritzing



THE PROGRAM


#define M1pin1 9  
#define M1pin2 10
void setup()
{
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
}
void loop()
{
 Clockwise();  // motor turns in clockwise direction
 delay(2000);  // Arduino sleeps for 2 seconds
 AntiClockwise(); // motor turns in anticlockwise direction
 delay(2000);
}

void Clockwise()
{
    digitalWrite(M1pin1, HIGH); // digitalWrite is a predefined function to control pin state.
    digitalWrite(M1pin2, LOW);
}  
void AntiClockwise()
{
 
    digitalWrite(M1pin2, HIGH);
    digitalWrite(M1pin1, LOW);
}



If You Are looking for speed control :
// You need to connect input wires to a pmw enabled pins on Arduino. These are identified by ~ sign on the arduino board. Notice 9 has a ~ before it on the board.


#define M1pin1 9       
#define M1pin2 10
int pmw=128;
void setup()
{
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
}
void loop()
{
 Clockwise();  // motor turns in clockwise direction
 delay(2000);  // Arduino sleeps for 2 seconds
 AntiClockwise(); // motor turns in anticlockwise direction
 delay(2000);
}

void Clockwise()
{
    analogWrite(M1pin1, pmw); // analogWrite is a predefined function to control pin state.
    digitalWrite(M1pin2, LOW);
}  
void AntiClockwise()
{
 
    analogWrite(M1pin2, pmw);
    digitalWrite(M1pin1, LOW);
}

//pmw can take values between 0 and 256. At 256, it will run at max speed. At 128, it will run at half the speed.