Temperature Sensor 18B20 with Arduino

Hello everyone, in today's post we are gonna have a look at how to interface temperature sensor Dallas 18B20 with Arduino. There are many temperature sensors available in market like LM35, DHT11 etc but personally I like Dallas18B20 most of all, as it gives the most accurate result up to four decimal points. It operates on single wire and sends all data through this wire. Another advantage of this wire is you can interface multiple sensors with a single data line. You should also have a look at How to use 18B20 in Proteus ISIS.

In today's post, we are gonna get value from this sensor and then print it over the Serial Terminal as well as LCD. We will get the values in degree centigrade. Its not much difficult to interface 18B20 with arduino and also an Arduino library is also availble, using which you can quite easily interface 18B20 with Arduino. Let's get started with interfacing of 18B20 with Arduino.

Note:

  • In today's post,we will show the values of temperature sensor over the LCD, the complete code is given below but I am not adding the circuit diagram of LCD I have already explain it in detail which you can check at Circuit Designing of LCD with Arduino in Proteus ISIS.

Interfacing of Temperature Sensor 18B20 with Arduino

  • As I explained earlier, it works on single wire and hence we are gonna need 1-wire library for Arduino along with 18B20 arduino library.
  • Download both of these libraries by clicking on the below buttons:

Download One Wire Library Download Dallas Temperature Library

  • After downloading the library, place it in the libraries folder of your Arduino Software.
  • Now restart your Arduino software and you will find the Arduino folder in the Examples section.
  • Next we need to interface our sensor 18B20 with Arduino so design your circuit as shown in below figure:
  • So, connect the sensor 18B20 with Arduino as shown in the above figure, connections are quite simple and are as follows:
    • Pin # 1 of 18B20 with GND
    • Pin # 2 of 18B20 with Pin # 2 of Arduino.
    • Pin # 3 of 18B20 with GND of Arduino.
    • Add a pull up resistor of 4.7k ohm at pin # 2 of 18B20.
  • Here's the images of hardware, we designed for this project, its a 20 x 4 lcd we have used:
  • Below image shows the small 18B20 sensor, used in this project, it looks small but very efficient.
 
  • Here's the image showing the complete project:
 
  • Now, copy below code and upload it in your Arduino board and open your serial terminal.
#include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); LiquidCrystal lcd(12, 11, 7, 6, 5, 4); void setup(void) { Serial.begin(9600); Serial.println("Welcome to TEP !!!"); Serial.println("www.TheEngineeringProjects.com"); Serial.println(); sensors.begin(); lcd.begin(20, 4); lcd.setCursor(5,0); lcd.print("Welcome to:"); lcd.setCursor(1,2); lcd.print("www.TheEngineering"); lcd.setCursor(4,3); lcd.print("Projects.com"); delay(5000); } void loop(void) { sensors.requestTemperatures(); Serial.print("Temperature : "); Serial.println(sensors.getTempCByIndex(0)); //lcd.clear(); lcd.setCursor(0,0); lcd.print("Temperature: "); lcd.print(sensors.getTempCByIndex(0)); lcd.print("C"); delay(1000); }
  • After uploading the code, when I start the project, it started showing the temperature values as shown below:
  • As you can see, its giving the temperature of my room which is 23.56 degree centigrade.
  • I have also designed a video for more demonstration which is given below:
  • It's quite a simple code and is self explanatory but still if you need help ask in comments and I will help you out.

Interfacing of Multiple Ultrasonic Sensor With Arduino

Hello friends, hope you are having fun and enjoying life. Today, I am gonna post about interfacing of multiple Ultrasonic sensor with Arduino. In the previous post, we have seen Interfacing of Ultrasonic Sensor With Arduino and in this post I have interfaced single ultrasonic sensor but in projects especially related to robotics, we have to interface multiple ultrasonic sensors. For example you have an obstacle detection robot, now in order to detect obstacle in front of robot you have to place once sensor on the front side but now you can't detect any object present on left or right side of your robot, so you have to place two sensors one on the left side of robot and one on the right side so in this project you need to use total three ultrasonic sensors, one on the front, one on left and one on right side of robot. Similarly, in another project I have to move the robot in a maze having walls on the side of robots, and my task was to move the robot straight within these walls without hitting the walls. In that case, I also used two ultrasonic sensors on both sides of robot and then applied PID algorithm in order to avoid hitting the walls. So, in short its a common practice to use multiple ultrasonic sensor with Arduino and today we are gonna have a look at how to do it.

I have posted about the basics of Ultrasonic sensor and how it works in my previous post so I am not gonna go into that detail. If you haven't read it then I recommend that you should first read Interfacing of Ultrasonic sensor with Arduino. Now, let's get started with Interfacing of multiple ultrasonic sensor with arduino, which isn't that difficult. :)

Note:

Interfacing of Multiple Ultrasonic Sensor With Arduino

  • Let me first summarize the working of ultrasonic sensor again. With ultrasonic sensor, what we need to do is to generate a trigger signal on its trigger pin for around 10 microsecond.
  • As soon as the ultrasonic sensor gets this trigger signal, it sends out an ultrasonic signal.
  • This ultrasonic signal then hits something and bounced back.
  • Now, in order to check this bouncing signal, we have to read the Echo pin and check for how long it remains HIGH, and on the basis of this duration we calculate our distance with the object.
  • This is the process for single ultrasonic sensor and when we are using multiple ultrasonic sensors, what we need to do is simply repeat the whole procedure for all the sensors one by one.
  • First of all, we will generate the trigger pulse for first sensor and the read its echo pin and get the distance, then we generate the trigger pulse for second sensor and read its echo pin and so on for the third.
  • So, here I am gonna use three ultrasonic sensor and the circuit diagram is shown below:
  • I have tried my best while designing this image to make it simple but as there are too much wires so it has become a little complex.
  • I am pointing out the pin configuration here so it will be easy for you to interface your sensors with arduino. The pin configuration is as follows:
    • Vcc of all sensors will go into +5V of Arduino.
    • GND of all sensors will go into GND of Arduino.
    • Trig Pin of first sensor into Pin # 3 of Arduino.
    • Echho Pin of first sensor into Pin # 2 of Arduino.
    • Trig Pin of second sensor into Pin # 4 of Arduino.
    • Echo pin of second sensor into Pin # 5 of Arduino.
    • Trig Pin of third sensor into Pin # 7 of Arduino.
    • Echo pin of third sensor into Pin # 8 of Arduino.
  • After connecting the pins as discussed above, now copy the below code and upload it in your arduino board.
  • After uploading the code in your arduino, open the Serial Terminal of Arduino software and you will start receiving the distances for all the three sensors.
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8

long duration, distance, RightSensor,BackSensor,FrontSensor,LeftSensor;

void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}

void loop() {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;

Serial.print(LeftSensor);
Serial.print(" - ");
Serial.print(FrontSensor);
Serial.print(" - ");
Serial.println(RightSensor);
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

}
  • The code is quite similar to the one we used while interfacing single ultrasonic sensor with arduino, the only thing we changed here is the repetition.
  • Before, we were using the same function SonarSensor() but calling it only once for our single sensor interfaced with arduino but now we are calling it three times for all the three sensors.
  • Its kind of a generic code, you can interface more sensors with it if you want and what you need to do is only calling this function for the next interfaced sensor.
That's all for today, I think we have posted a lot on the ultrasonic sensor so I am not gonna post any more tutorial on this sensor and now I will start writing on some other sensor. You should also have a look at Arduino Projects for Beginners. Thanks for reading and share it with your friends and help us grow. :)

Interfacing of Ultrasonic Sensor With Arduino

Today, we are gonna have a look on How to Interface Ultrasonic Sensor with Arduino. Few days ago, I have posted a complete tutorial on How to Use Ultrasonic Sensor Library in Proteus and later I have posted different examples on How to Simulate Ultrasonic Sensor in Proteus. Those posts were about Proteus Simulations and weren't about hardware interfacing, so I thought today let's interface it in hardware.

Simulation is a good starting point for projects but they are really far away from real world. It happened to me a lot of times that my simulations are working perfectly fine but when I design the same circuit in hardware then it says no I am not gonna work. :) So, the bottom line is never trust simulations, unless you properly test it on hardware. So, today I am gonna interface an Ultrasonic sensor with arduino and will check its output on the Arduino Serial Terminal.

1. Introduction to Ultrasonic Sensor

  • "Ultrasonic Sensor HC-SR04 is a simple sensor which emits Ultrasonic Radiations from its transmitter and is used for measuring the distance between sensor itself and any obstacle in front of it. The sensor has a transmitter and a receiver on it."
  • This sensor consists of four pins, which are:
    • Vcc (+5V) : You need to provide +5V at this Ultrasonic Sensor HC-SR04 Pin.
    • Trig (Trigger) : It's a trigger Pin where we need to provide a trigger after which this sensor emits ultrasonic waves.
    • Echo : When Ultrasonic waves emitted y the transmitter, hit some object then they are bounced back and are received by the receiver and at that moment this echo Pin goes HIGH.
    • GND : We need to provide ground to this PIN of HC-SR04 Ultrasonic Sensor.
Note:
  • If you haven't bought your components yet for this project, then you can buy them from these reliable sources:
[ultimate_spacer height="13"]

[ultimate_spacer height="13"]

  • Trigger pin is an output pin while the Echo pin is an input pin, we will discuss them in Working section in detail.
  • Moreover, it requires +5V to start operating.
  • It is normally used to detect objects in front of it or to measure the distance between different objects.

2. Working of Ultrasonic Sensor

  • Its working is quite simple, as discussed above, it has a trigger and an echo pin.
  • A signal of +5V is sent over to Trigger pin for around 10 microseconds in order to trigger the sensor.
  • When ultrasonic sensor gets a trigger signal on its trigger pin then it emits an ultrasonic signal from the transmitter.
  • This ultrasonic senor, then goes out and reflected back after hitting some object in front.
  • This reflected ultrasonic signal is then captured by the receiver of ultrasonic sensor.
  • As the sensor gets this reflected signal, it automatically make the Echo pin high.
  • The time for which the Echo pin will remain HIGH, depends on the reflected signal.
  • What we need to do is, we need to read this time for which the echo pin is high, which we are gonna do in our next section.
  • So, let's have a look at Ultrasonic Sensor Arduino Interfacing.

3. Interfacing of Ultrasonic Sensor With Arduino

  • Now we have seen the working of Ultrasonic sensor, so we have some idea what we need to do in order to get the values from it. Let's now have a look at Ultrasonic Sensor Arduino Interfacing.
  • First of all, we need to generate a signal of 10 microsecond and then send it over to trigger pin.
  • After sending the trigger pin we then need to read the echo pin and wait for it to get HIGH.
  • Once it got HIGH then we need to count the time for how long it remained HIGH.
  • On the basis of this time, we are gonna calculate the distance of the object from the ultrasonic sensor.
  • So, first of all, interface your ultrasonic sensor with arduino as shown in below figure:
  • Now, use the below code and upload it your arduino board. After uploading the code, open your serial terminal of Arduino software and you will start receiving the values.
#define trigPin1 8
#define echoPin1 7

long duration, distance, UltraSensor;

void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}

void loop() {
SonarSensor(trigPin1, echoPin1);
UltraSensor = distance;
Serial.println(UltraSensor);
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
delay(100);
}
  • Now if you check in the SonarSensor() function, we are generating a pulse of 10 microsecond and sending it to trigPin, which is the trigger pin of our ultrasonic sensor.
  • After sending this pulse weare using a funcion pulseIn() , its a builtin arduinofunction and is used to check for how long the echoPin remains HIGH.
  • This value is further saved in the duration value and after that we have divided this duration by 2 because the pulse is first sent and then received so in actual it covers double distance, so we need to divide it by 2 in order to get distance between object and the sensor.
  • Furthermore, it is again divided by 29.1, which is basically the speed of ultrasonic sound and finally we saved it in a variable named distance which is now in centimeters.
  • After uploading the sketch in Arduino, you need to open the Serial Terminal and you will start receiving the values of distance.
That's all for today. I hope you have enjoyed this Interfacing of Ultrasonic Sensor with Arduino. It wasn't that difficult, in our coming post we are gonna Interface Multiple ultrasonic sensors with Arduino and will get their values on the serial terminal. Till then Take care and have fun !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir