Arduino

My Blog

Latest blog


ULTRASONIC SENSOR AND ITS INTERFACING


In this session, we will see How to use Ultrasonic Sensor with Arduino . Ultrasonic Sensor is used to calculate the distances of the obstacle. It works on the principle of SONAR.
Ultrasonic Sensor senses the distance of an object by sending the ultrasonic sound at 40,000 Hz, which travels through the air and if there is an object or obstacle on its path it will reflect or bounce back to the receiver of the module.
·        The common name of Ultrasonic Sensor is HC-SR04.
·        Operating Voltage of this sensor is +5V.
·        It can measure distance from 2cm to 80cm.

·        It contains 4 pins – VCC, Ground, Echo, Trigger.


·                                Trigger pin is the input pin and it sends the ultrasonic waves.
·                                        
                  Echo pin is the output pin and it receives the bounced or reflected waves.

FORMULA TO CALCULATE DISTANCE

Speed of sound: v=340 m/s,   v=0.034cm/μ s

Time = Distance / Speed:

                    t   =   s/v   =   10 / 0.034 =   294 μ s
                    Distance:   s   =   t   *   0.034 / 2



          CONNECTING THE SENSOR WITH ARDUINO


    
Things we need:

  1. Arduino UNO (Amazon)
  2. Ultrasonic Sensor (Amazon)

 This sensor connection to the ARDUINO is discussed in the following steps:
·        
  • There will be 4 pins present on the ultrasonic sensor.
  •  Initially the VCC pin of the Ultrasonic Sensor is connected to the VCC(5V) pin     of the ARDUINO by using the male to female wire.
  • Then the GROUND pin of the Ultrasonic Sensor is connected to the GND of the   ARDUINO by using the male to female wire.
  •  Finally, the TRIGGER and ECHO pins of the Ultrasonic Sensor is connected to     the Digital pins of the ARDUINO, excepting the 0 and 1 pins since they                 are used for transmission and receiving purpose.
  •  Now the power supply is given to the ARDUINO for the functioning of the         Ultrasonic Sensor and the ARDUINO Development Board.


The complete interfacing of the Ultrasonic Sensor with ARDUINO is done perfectly, the only left over task is to type the program and dump the program into the ARDUINO Development Board.




ARDUINO PROGRAM OF HC-SR04

// defines pins numbers
const int trigPin = 2;
const int echoPin = 4;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
After the complete writing of the above program in ARDUINO IDE , you can dump the code into the ARDUINO board.


 FUNCTIONING OF HC-SR04

After the successful dumping of code into the ARDUINO board, the output values of the Ultrasonic Sensor can be visible in the serial monitor in the computer screen. When a obstacle is detected in the path of Ultrasonic Sensor , then the distance of the obstacle from the sensor is displayed on the serial monitor in the ARDUINO IDE software window on the computer screen.

EXPLAINATION OF THE PROGRAM

Initially the TRIGGER pin and the ECHO pin is connected to the pins 2 and 4 respectively in the ARDUINO BOARD , this means the ARDUINO board gets the input and output values from the 2 and 4 pins connection of the TRIGGER AND ECHO pins.
·                  The Variables duration and distance are declared.
·        Sets the TRIGGER Pin as an Output and ECHO Pin as an Input.
·        Then establishes the serial communication with baud rate of 9600
·        Then in the loop, TRIGGER pin is kept off (low) for 2 microseconds.
·        Now the TRIGGER pin is kept on (high) for 10 microseconds and then kept off .
·        Then the ECHO pin is kept on (high) to read the time taken by the wave to return           back and is stored in duration.
·        Now finally, the distance is calculated by using the appropriate formulae.
·        And the distance is printed on the Serial Monitor.


CONCLUSION

The above discussed information about the Ultrasonic Sensor is all about the interfacing and functioning of the Sensor with ARDUINO Development Board. In this way the HC-SR04 component can be connected to the ARDUINO and can be used for wide range of applications in the Autonomous Robotic Projects.




Previously we learnt how to decode IR signals using Arduino and how to use  these signals to control LEDs. It was pretty basic setup and a good project to get started. Today we will use that knowledge to build a simple robot car which will be controlled using an IR remote. I suggest you check out "Everything you need to know about motor drivers" article to get a better understanding on how to control the motors.

With out any further ado, lets get right into it.

Requirements: 

For any projects we need some materials. Here I have listed all the hardware and software requirements for this project.

Hardware:

  1. Arduino Uno   (Amazon Link)
  2. Arduino Motor Shield v1.0   (Amazon Link)
  3. 2 x Geared Motor   (Amazon Link)
  4. 2 x Wheels   (Amazon Link)
  5. Caster Wheel   (Amazon Link)
  6. 2WD Chassis   (Amazon Link)
  7. 1838 IR Receiver   (Amazon Link)
  8. 3 x Female Headers   (Amazon Link)
  9. 4 x 3.7v Li-ion batteries   (Amazon Link)
  10.  Li-ion Battery Holder   (Amazon Link)
  11. LM2596 5v Voltage Regulator   (Amazon Link)
  12. DC Power Jack   (Amazon Link)
  13. IR Remote   (Amazon Link)
NOTE: Make sure you use the same IR receiver as I have used here. Also The 2WD chassis are not required if you will make your own platform as I have.

Software:

  1. Arduino IDE: For writing and uploading code
  2. SketchUp : For designing Chassis  
That's all the things we need for this project, now lets move on to next step, designing chassis.

Building Chassis:

Building chassis is an optional part here, you can just skip this step and get yourself a 2WD robot chassis. I have built myself one using foamboard, The design is pretty simple you can find a SketchUp file below:

arduino robot


You can make your own design as you want, just make sure all the components fit in properly.

IR Robot CAD File

Making Circuit: 

The circuit is pretty straight forward, The motor shield is placed on top of the Arduino board and the IR receiver is connected to A0 pin. Refer the following image.

1838 IR receiver

Here I have soldered the pins of the receiver directly to female headers. Which can be connected to the motor shield directly as shown below:

arduino motor shield


Note:-  If you are not familiar with 1838 IR receiver, check out this article.

Now about the power supply, we need two. One for Arduino (9-12v) another for Motorshield (12-30v). For our motor requirements we need atleast 12v so we will use a 4cell 18650 battery pack. 4x3.7 will give us 14.5v which is enough for motor drive.

But now the problem is about arduino, we cannot power the microcontroller with more than 12v. To solve this problem I have used a DC to DC voltage regulator. 
Check the power supply circuit diagram given below for better understanding.


This is pretty simple circuit and needs no further explanation. But before you connect the output to arduino, use a multi meter to check if the voltage is between 9-12v. You can adjust the voltage by adjusting the screw on the trimmer. 

Coding:

Now we can write the code to control our robot. 
First get any IR remote you wish to use and decode its IR signals. If you have no idea how to do that, check out this article

Once you have successfully decoded the IR signals of the buttons on your remote, you should have some hex numbers as: 20DF02FD, 20DFE01F, etc. 

We will use these codes in the script below:

Note: Make sure that you have the ""IRremote" and "AFMotor" libraries installed in IDE.

#include <AFMotor.h>
#include <IRremote.h>
int RECV_PIN = A0; //data out of IR receiver connects to pin A0 
AF_DCMotor motor1(3); 
AF_DCMotor motor2(4);
IRrecv irrecv(RECV_PIN); 
decode_results results; 

void setup() 

 Serial.begin(9600); 
 irrecv.enableIRIn(); // start the receiver 
 motor1.setSpeed(100); 
 motor2.setSpeed(100); 


void loop() 

 if (irrecv.decode(&results)) 
{ Serial.println(results.value, HEX); 
 irrecv.resume(); // Receive the next value 
 if (results.value==0x20DF02FD)
{ //Go Forward 
 motor2.run(FORWARD); 
 motor1.run(FORWARD); 
 motor1.setSpeed(100); 
 motor2.setSpeed(100);

if (results.value==0x20DF827D)
{ //Go Backward 
 motor1.run(BACKWARD); 
 motor2.run(BACKWARD); 
 motor1.setSpeed(100); 
 motor2.setSpeed(100);

 if (results.value==0x20DFE01F)
{ //turn left 
 motor1.run(FORWARD); 
 motor2.run(BACKWARD); 
 motor1.setSpeed(80); 
 motor2.setSpeed(80);

if (results.value==0x20DF609F)
{ //turn right 
 motor1.run(BACKWARD); 
 motor2.run(FORWARD); 
 motor1.setSpeed(80); 
 motor2.setSpeed(80);

 if (results.value==0x20DF22DD)
{ //stop
 motor1.run(RELEASE); 
 motor2.run(RELEASE);

 irrecv.resume(); 
}
}

In the above codes just replace the codes with yours and make sure to put the '0x' prefix.
Now you can power up the robot and start controlling it with your IR remote.

Final Note:

Here is a look at the robot I made, you can go ahead and make your own design. 

arduino robot


Also you might like to increase the speed of the robot, to so that just increase the number in the setSpeed() function. It can be anywhere between 0-255, where 0 will stop the motors completely and 255 will set the highest speed as per the motor.

With that been said, you can now make robot and play around. If you have any questions or doubts, feel free to comment down below.
Previously we learned how to control DC motors using arduino. We used a motor driver IC named L293D for the purpose. Although it does get the job done, it isn't the best way to do it. The IC can get hot rather quickly and there is no filtering of motor noise which could cause problems in RF communications and data transfer. 

To avoid these problems we can use a motor drive board/module. Motor drive boards have a driver IC as the heart with a heat sink and some other electronic components to reduce the motor noise. Let's see what a motor driver is, how it works and how to use it for our projects.

What is a Motor Driver Board?

A module with a motor driver IC which helps us control speed and direction of DC motors is called a motor driver board. There are a variety of motor driver boards in the market. Most popular being L298N and L293D based motor drivers. 

These boards are compatible with all arduino, esp and similar microcontrollers. In this article we will perticulerly look into 2 very popular motor drivers, The L298N Driver and L293D based motor controller shield.

So let's see these boards in more detail:

L298N Motor Driver:

This board is very popular in the makers community for its smaller size and power. The IC has a heat sink already which makes the board highly efficient. 


L298N motor driver board
L298N is dual H-bridge IC, which means we can control 2 motors uisng this IC.
Here is a list of specification of the driver:



  • Dual H-Bridge Drive Chip: L298N
  • Logical Voltage: 5V
  • Drive Voltage: 5V-35V
  • Logical Current: 0-36mA
  • Drive current: 2A (MAX single bridge)
  • Max Power: 25W
  • Dimensions: 43 x 43 x 26mm
  • Weight: 26g
Using this motor driver we can control 12v-30v motor with max current of 2A. Using driver is pretty similar to using bare IC. Check out this tutorial for more information about that.

L293D Motor Shield:

Shields are something you use with Arduino. These boards stack on top of arduino and we can use them with libraries. It reduces the mess created by wires and saves a lot of space.
Here we will see the popular Arduino Motor Shield V1. 

arduino motor shield motor driver
This motor shield has two L293D ICs which control two DC motors each, hence we can control four DC motors or two stepper motors using this shield. Along with that we can also control two servo motors with this shield.

  • Double H Bridge Drive Chip: L293D
  • Logical Voltage: 5V
  • Drive Voltage: 5V-36V
  • Logical Current: 0-36mA
  • Drive current: 1.2A (MAX single bridge)
  • Max Power: 25W
  • Dimensions: 69 x 53 x 14.3mm
This motor shield just stacks on top of arduino and using a library called AFMotor.h we can easily control motors and servos using it. 

How To Use Motor Shield?

Motor shield can be stacked upon our Arduino uno. Before coding, we need to install the library.
To install library first open the IDE and goto >> Tools >> Manage Libraries
In the search box type "Adafruit Motor Shield" and install the first library.


After the library is installed, goto >> Files >> Examples >> Adafruit Motor Shield Library >> Motor Test. 

Next connect the Arduino to PC and upload the code. Now connect a DC motor to the M4 terminal and see how the motor works. You will notice that the motor slowly spin in one direction gradually gaining speed and then it does same in other direction. 

That's all for this article, now you know how to control a motor using a motor shield. You can use this for testing different codes in the example and learn more. In future we will use this to create an arduino based robot. Till then keep experimenting.

If you like my tutorials and want to stay updated for more, make sure you follow my blog. If you have any questions, feel free to comment down bellow.
In the previous article we learned about IR remotes, how they work and how to decode signals of any IR remote. If you haven't checked it already, here is a link. I recommend you read it first to better understand this tutorial. 

In this tutorial we will see how to use the decoded values to turn devices on/off, for example LEDs. Here I will demonstrate it using two LEDs. So without wasting any more time, lets get started.

Requirements: 

Now that everything is gathered, we can start with making the circuit on a breadboard.

Circuit:

IR automation, home automation, arduino project

Make the connections as in the image above. Similar to the precious tutorial the IR LED is connected to pin 2. The LEDs are connected to Pin 8 and 9. After making the connections we can upload the code and start controlling the LEDs.

Coding:

Before you upload the code, First you need to install the IRremote library. To know how to do that, check out this tutorial. After the Library is installed you can refer the code given bellow:
#include<IRremote.h>
int IRPIN = 2;  //Receiver Pin  
int Green = 8;  //Green LED
int Red = 9;  //Red LED

int flag1 = 0; //Indicates state of Green LED
int flag2 =0; //Indicates state of Red LED

IRrecv irrecv(IRPIN); //Reads the signals from receiver
decode_results results; //saves decoded results 

void setup()
{
    Serial.begin(9600);
    Serial.println("Enabling IRin");
    irrecv.enableIRIn(); //Starts receiving
    Serial.println("Enabled IRin");

    pinMode(Green, OUTPUT);
    pinMode(Red, OUTPUT);
    pinMode(Green, LOW); //Turns LEDs off by default
    pinMode(Red, LOW);
 }

void loop()
{
    if(irrecv.decode(&results))
   {
      Serial.println(results.value, HEX);
      irrecv.resume();
      if(results.value==0xFD08F7) //If this code is received
     {
        if(flag1 == 0) //If flag state is 0 (LED is off)
       {
          digitalWrite(Green, HIGH); //turns LED on
          flag1 = 1; //Sets flag as 1
       }
       else if(flag1 == 1) //if flag is 1 (LED is on)
      {
         digitalWrite(Green,LOW); //Turns LED off
         flag1 = 0; //sets flag 0
       }
     }
     if(results.value==0xFD8877)
    {
       if(flag2 == 0)
       {
         digitalWrite(Red, HIGH);
         flag2 = 1;
       }
       else if(flag2 == 1)
       {
          digitalWrite(Red,LOW);
          flag2 = 0;
        }
     }
     irrecv.resume(); //Ready to receive next data
 }
}

In the code above we have used the codes generated by IR remote. The algorithm is fairly simple I recommend you check this tutorial to learn about the flags and how to use it.
Copy and upload this code using Arduino IDE.

Testing:

After uploading the code you can start turning on off the LEDs using IR remote. 


I hope this tutorial was helpful and you can implement this in your projects like home automation. Just replace LED with Relays and you can turn on/off AC appliances.
Thats all for this tutorial, if you like it and want more feel free to follow/subscribe. 

In next tutorial we will make IR controlled Robot until then, keep experimenting ;)
IR technology is nothing new it has been around for quite a while now. These days you can find IR remotes controlling everything from TVs, Sound systems and Air conditioning systems to Toys, Home automation and much more. So its not a surprise to find some old IR remotes laying around in the house. Instead of throwing it away we can do much better things. Maybe automate our homes or build an IR controlled Robot car. But in order to do this first we must understand how this technology works.

In this article we will see how IR remotes works and how we can decode their signals for reusing. So without wasting anymore time, lets get right into it.


What is IR Technology:

First question that comes to mind is what is IR technology? IR stands for Infrared. It is a wavelength of light which is invisible to human eyes. IR wavelength ranges between 700nm-1mm, humans can only see light between 400nm-700nm. There are tons of applications for IR light which you can read about here. We will focus on IR for remote controls.

How IR Remotes Work:

In IR remotes, an infrared LED is used to transmit data (in form of pulses). This data is sent to the IR receiver on the receiving end (TV, toys, etc). The pulses are decoded by the receiver and the action is taken. The actions depend on the codes which differ based on the frequency of light emitted by the IR LED.

You can't see IR light with your naked eyes but there is a way to check how the LED sends data. For this you will need a TV remote and your smartphone. Just open your camera and look at the LED on the TV remote as you press a button.

ir remote, ir led

You will notice the LED flicker, the LED might seem solid on but its just flickering at a very high frequency. So to use the IR remote we must find out what code the buttons are sending via the LED. 

Requirements:

After you have everything we can start with setting up the IDE.

Setting up IDE & Uploading Code:

To decode the IR signals, we need to install a library in our IDE. To install this library follow the steps given bellow:
  • Open IDE and goto >> Tools >> Manage Libraries. A pop up windows should show up. Here enter "IRremte" and scroll down, next install IRremote by shirriff.
how to install library in arduino ide
  • After installing the library restart the IDE and write the following code:
#include <IRremote.h>

int IRPIN = 2;

IRrecv irrecv(IRPIN);

decode_results result;

void setup()
{
  Serial.begin(9600);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn();
  Serial.println("Enabled IRin");
}

void loop()
{
  if (irrecv.decode(&result))
  {
    Serial.println(result.value, HEX);
    irrecv.resume();
  }
  delay(500);
}

upload the code to arduino. Now lets make connections.


Connections:

Before connecting the IR receiver to the arduino make sure you know the pin outs. To find the pinouts just enter the name/number of receiver along with pinouts in google for example: "1838 pinouts". It will show you the pins and its purpose. As in this example I have used a 1838 receiver. Here is a pin diagram:

1838 IR receiver

Now that we know the pinouts we can make the connections as shown in the image below:

IR receiver with arduino

Connect the Vcc to 3.3v, Gnd to Gnd and Signal to pin 2. Now we can start decoding the IR signals.

How to Use it?

So now we have a sensor connected to our Arduino and the code is uploaded. To decode a Remote first open the Serial monitor. To open serial monitor goto >> tools >> Serial Monitor. Opening the serial monitor will reset the board and now you can point the Remote at the receiver and press the button you want to decode.


Note down the Hex code generated for the buttons. We can use these codes for multiple projects like automation, remote controlled car etc. 

That's all for this project, now you can decode any IR remotes and reuse it for projects. Learn how to use these codes in next tutorial. Till then, keep experimenting.

If you like this tutorial and want to learn more, make sure you follow and share this article with your friends ;)

In previous tutorials we learnt "How to use a pushbutton with Arduino" and "How to control DC motor with Arduino". In this article we will see how to control Servo Motors. Servo motors are very useful in many projects, mostly robotic. Before we can control one we have to know what a Servo motor is and how it works.

Introduction To Servo Motor:

There are many types of servo motors in the market in a variety of form factors. But all work in a similar fashion. Unlike a DC motor which rotates 360° a Servo motor just travels 180° and has a gear system which increases the torque.



servo

In above image you can see the internals of a standard hobby servo. It consists of 3 important parts, the motor, control circuit and drive gears. All of these are enclosed in a case. The circuit is a closed loop motor controller with a feed back potentiometer. The potentiometer sends feedback to the circuit which tells the current position of the servo shaft.

Pinouts:

servo

As you can see in the image Servo has 3 pins. Signal, Vcc and Gnd, these pins can be recognized based on the colour of the wire. Usually Orange/Yellow is the signal wire, Red is Vcc and Black is Gnd.

Now that you are familiar with pins of servo we can move on to see how we can control a servo using Arduino. Before that here are some things you will need:

Requirement:

Now that you have gathered all the required components, we can move forward to control the servo.


Controlling Servo Using Arduino:

To control servo we need PWM signals, PWM stands for Pulse Width Modulation. In PWM a digital pin is used to generate a square wave. It is a signal created by high and low pulses with an equal interval of time. The duration of on time / High signal is called as Pulse Width and controlling the duration can create different frequencies.


Arduino PWM
Arduino has 6 PWM pins (3,5,6,9,10 & 11). As arduino is a 8-bit controller its PWM is 0-255, 0 being lowest and 255 being highest. When the output is 255 the pin is at 5v constant, for 127 it is 2.5v and so on.

To control the Servo we will use this feature of the arduino. So lets start with something simple.

  1. Simple Servo Control With Arduino:




First connect the servo to arduino as shown in the figure. The Vcc is connected to 5v, Gnd is connected to Gnd and Signal pin is connected to pin 9. The servo is powered directly from arduino but if you are planning to use more than one servo, I suggest using an external power supply. For more information of power requirements of your servo check it's data sheet. 

Now refer the code bellow:

#include<Servo.h> //import the servo library

Servo myServo; //set servo variable as myServo


void setup()
{
   myServo.attach(9); //connect servo signal to pin 9
}

void loop()

{
   for(int i=0; i <= 180; i++) //goes 0 to 180 real quick ;p
   {
      myServo.write(i); //writes the value to turn servo 
      delay(50); //50ms delay
   }
   
   for(int i=0; i>= 0; i--) //goes form 180 to 0 
   {
      myServo.write(i); //writes the value to turn servo
      delay(50); //50ms delay
   }
}

Upload this code to uno. You will notice the servo sweeps from end to end. 



This code is best for checking if a servo is working. But for most projects we need a precise way to control servo angle. For that we will use a potentiometer.

2. Controlling Servo Using Potentiometer:

servo motor

In this circuit the servo connections are similar to previous one. Here we have added a 10k ohm potentiometer (variable resistor). The central terminal of pot is connected to A0 pin, other two pins are connected to 5v and Gnd. The pot will be used to generate analog values which we will map to the servo. Thus the change in resistance of the potentiometer will help us control the movement of servo precisely.

Lets take a look at the code:

#include<Servo.h>

Servo myServo;
int potPin = 0; //Set a variable named potPin as A0

void setup()
{
  myServo.attach(9);
}

void loop()
{
  int val = analogRead(potPin); // read the pot value,save it in val
  val = map(val, 0, 1023, 0, 180); //scale the pot readings to 0-180
  myServo.write(val); //set the servo position according to val
  delay(50); //50ms delay
}

Upload the code to arduino and you should see the servo move to a specific position. It depends on the position of potentiometer. Now turning the pot will move the servo head.


The above method combined with other projects can be used for many applications like RC toys, robots and much more. We will see some projects related to it in future. 

That's all for this tutorial. If you have any doubts or questions, feel free to ask in the comment. If you like my tutorials do follow for more. 

See you in next tutorial, until then keep experimenting... ;)

Contact Us

Contact With Us

Followers

Translate

Get in touch with us.

  • +91 7276861817
  • projecthub.in@protonmail.com