Showing posts with label ultrasonic sensor. Show all posts
Showing posts with label ultrasonic sensor. Show all posts

A Guide For Interfacing Analog Sensors With Arduino

By // 6 comments:
With Arduino, almost everything seems to be too friendly when it comes to interfacing various sensors, shields, add-ons or any other utility devices. Just because the Arduino coding environment is so user-friendly, anyone can do it. So in this post, I will just show you how to hook up your sensors to Arduino and get them running. Let's start with the very basics....

sensor interfacing with arduino
Sensors
What is Sensor
Sensors are those electronic components which convert physical data into electronic data. This data is in analog format and is fed to the microcontroller on the Arduino board. The microcontroller has inbuilt ADCs (Analog-2-Digital Converter) which processes this data and converts it into digital format.

And once you have received the (electronically converted) physical data, you can make your Arduino perform as you want.


On your Arduino board, there are analog pins named as A0, A1, A2, A3, A4, A5. The number of these pins may vary depending on your Arduino board. For UNO, there are only 6 analog pins while for MEGA there are 16. And remember this thing, any sensor (or other components) that gives analog data and you wish to process it, you'll have to connect it to your analog pins only. For now, I would say SENSORS would always be connected to analog pins.

Connection of Analog Sensor with Arduino
All sensors have their own method to connect with Arduino. Some of them need pull-up resistors, some need a certain power supply to use them.  But any sensor has generally 3 pins to connect to Arduino or other development board. These pins are. 
1. +Vcc
2. Signal
3. Gnd
Hook up the +Vcc to 5v (or 3.3v if sensor demands it) on your Arduino board.
Connect the Gnd pin to Ground pin on your Arduino.
Connect the Signal pin to any of your Arduino's analog pins. In our case, say A1.

Code for Interfacing Analog Sensor with Arduino
Below code is general code for interfacing analog sensors with Arduino. Write this code in your Arduino IDE to start playing with analog sensors.
void setup()
 {
  Serial.begin(9600);                            // initialize serial communication at 9600 bits per second:
  }

void loop() 
{
  int sensorValue = analogRead(A1);            // read the input on analog pin 1:
  Serial.println(sensorValue);                       // print out the value you read:
  delay(1);                                                      //this  delay in between reads for stability 
}

After burning this code to your Arduino, open the serial monitor and see the analog-2-digital converted data on your serial terminal. Basically, it is 10-bit data since the inbuilt ADC of your Arduino is a 10bit ADC.You will get the reading on your serial monitor.

That's all for now. Hope you got everything out of that blog.I'll try to bring something new next time. Till then........ keep learning and keep prototyping.

If You have got any problem or feedback then comment below. I will be right back to you soon.

Be sure to buy the stuff mentioned in this blog by clicking on the link below-

The vendor's offering interesting and working hardware at quite handy rates. Go give it a try! :)

Arduino Distance Measurement Using Ultrasonic Sensor

By // 3 comments:
We all know about the ultrasonic sensor and also wanted to play with it, but don't know how ultrasonic sensor works. So In this article, we will see  how to use an ultrasonic sensor with your projects. The ultrasonic sensor is very popular among arduino hobbyist and there are so many projects which you will be able to do at the end of this article.

Description of HC-SR04 Ultrasonic Sensor
ultrasonic sensor
Ultrasonic Sensor can measure the distance from 1" to 13 feet( 1 cm to up to 4metre) with accuracy up to 3mm. A hc-sr04 ultrasonic sensor has 4 pin GND, VCC, Echo and Trigger Pin. Trig pin generate an ultrasound by setting trig pin on the high state of 10 microseconds. Echo pin produce an output in a microsecond when ultrasound returns back from the object to echo pin.

We can calculate the distance by using simple formula Speed=(Distance/Time). The speed of the sound is 340m/s and thus by calculating ultrasound travel time we can calculate the object distance from the ultrasonic sensor.

Required Components
  1. 1 Arduino UNO board( Buy from here Arduino )
  2. 1 HC-SR04 ultrasonic sensor ( Buy from here sensor )
  3. Jumper wire
  4. Breadboard

Circuit Diagram
Ultrasonic sensor pin connected to arduino pin in a very simple manner as shown in the image below. Here we use a voltmeter as a reference because we can't place a real object before the LDR. So when we increase or decrease the voltage level of voltmeter then its corresponding value also changed.
Arduino           Ultrasonic sensor
Vcc                      Vcc
Pin 5                   Trig
Pin 4                   Echo
Gnd                     GND 
ultrasonic_sensor circuit
Circuit
Source code
Here first we define the trig pin and echo pin of the ultrasonic sensor and they are pin no 5 and pin no 4 respectively. Here we define Trig pin as output and Echo pin as an input. After that, we defined three long variable name duration, cm and inches. In the loop first we set the Trig pin Low for few microsecond and after that, we set it in the high state for 10 microseconds. We read the travel time by ultrasound by using the pulseIn() function.
ultrasonic sensor source code
Source Code
For getting the exact distance we multiply .034 in duration and then divide it by 2 because ultrasound travels forward and backward. At the end we use Serial.print() function to print the value in the virtual terminal.You can download the source code by clicking on the link.

Video Demonstration 


Hope you like this tutorial on the ultrasonic sensor. If you have got any problem using the ultrasonic sensor or its proteus simulation then feel free to comment below or if you have any suggestion about this tutorials and let me know by your valuable comments.