Arduino With Ultrasonic Sensor and Temperature Sensor
by hariharan02 in Circuits > Arduino
264 Views, 3 Favorites, 0 Comments
Arduino With Ultrasonic Sensor and Temperature Sensor
Lets see how the connection are made between Arduino with ultrasonic sensor and temperature sensor
MATERIALS REQUIRED
1.Arduino UNO
2.Ultrasonic Sensor (HCSR034)
3.Temperature sensor (DS18B20)
Connection
Connect the power and ground of sensors with Arduino
For Ultrasonic sensor :
echo_Pin - digital pin 2
trig_Pin - digital pin 3
For Temperature sensor :
ONE_WIRE_BUS_PIN - digital pin 4
CODE
/********Library header files*****/
#include
#include
/*********PIN details*****/
#define ONE_WIRE_BUS 4
#define echoPin 2
#define trigPin 3
/*********Temp communication****/
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
long duration;
int distance;
float Celcius=0;
float Fahrenheit=0;
/********Setup configuration****/
void setup(void)
{
sensors.begin();
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
/*******Process and Output****/
void loop(void)
{
sensors.requestTemperatures();
Serial.print("Temperature: ");
Celcius = sensors.getTempCByIndex(0);
Fahrenheit = sensors.toFahrenheit(Celcius);
/********Temperature******/
Serial.print("Celcius");
Serial.print(Celcius);
Serial.print("Fahrenheit");
Serial.print(Fahrenheit);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
/*********distance******/
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}