Temperature and Distance reading with active LED
This project monitors the Temperature, and distance. The LED is an indictor that the robot stop at a distance 10cm or less.
Components and supplies
1
LCD 16x2 + extras - White on Blue
1
Thermistor
1
Resistor 10k ohm
1
Jumper wires (generic)
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Arduino Mega 2560
1
Resistor 220 ohm
1
5 mm LED: Red
1
Single Turn Potentiometer- 100k ohms
Project description
Code
Temperature and Distance Monitoring
arduino
1/* 2 Temperature and Distance Monitoring 3 Measures the distance and temperature every 30 secs, and turn on the red LED when the distance is less than 10 cm. 4 You will need to download the NewPing and Liquid Crystal libary from libary manager. 5 */ 6// Unltra Sonice test 7#include <NewPing.h> //Libary used for the ultra sonic 8#include <LiquidCrystal.h> //Libary used for the LCD screen 9//Define Constants 10#define echoPin 4 // attach pin D4 Arduino to pin Echo of HC-SR04 11#define trigPin 3 // attach pin D3 Arduino to pin Trigger of HC-SR04 12#define Max_DISTANCE 400 // Set the max distance for the Ultra sonic 13#define ledPin 2 // LED pin D2 14NewPing sonar(trigPin,echoPin, Max_DISTANCE); 15 16//Define Variables, Variables will change as the program is ran: 17float duration, distance; 18int iterations = 10; 19int tempPin = 0; 20int ledState = LOW; // ledState used to set the LED at the start of the program 21// BS E D4 D5 D6 D7 22LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Pins for the LCD screen. These can be changed 23 24void setup() 25{ 26 //Set Pins and serial port printout 27 Serial.begin(9600); // Serial Communication is starting with 9600 of baudrate speed 28 Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor 29 Serial.println("with Arduino UNO R3"); 30 // Set the row and columns for the LCD screen 31 lcd.begin(16, 1); // Set column for distance display 32 lcd.begin(16, 2); // Set column for Temp display 33 34 // set the digital pin as output: 35 pinMode(ledPin, OUTPUT); 36} 37 38// Main Program Loop this will run until program is halted 39void loop() 40{ 41 //Begin Ultra Sonic Code and print to LCD screen 42duration = sonar.ping_median(iterations); 43distance = (duration / 2) * 0.0343; 44 45// Displays the Output 46 lcd.setCursor(0, 1); // set the column on the LCD 47 lcd.print ("Dist: cm"); // Displays the distance on the LCD 48 49 /* Checks that the distance is greater than or equal to 400 cm, 50 or if the distance is less than or equal to 5 cm 51 */ 52 if (distance>= 400 || distance<= 5) { 53 lcd.setCursor(0, 1); 54 lcd.print("Out of range"); // Displays the out of range on the LCD 55 } 56 else { 57 lcd.setCursor(5, 1); // set the column on the LCD 58 lcd.print (distance); // Displays the distance on the LCD 59 } 60// Serial monitor display this is mainly for debugging 61 Serial.print(" Duration: "); // Displays the distance on the Serial Monitor 62 Serial.print(duration); // Displays the distance on the Serial Monitor 63 Serial.print(" Dist: "); // Displays the distance on the Serial Monitor 64 Serial.print(distance); // Displays the distance on the Serial Monitor 65 Serial.println(" cm"); // Displays the distance on the Serial Monitor 66 67 // TEMP monitoring code 68 int tempReading = analogRead(tempPin); 69 // Temp conversion formula 70 double tempK = log(10000.0 * ((1024.0 / tempReading - 1))); 71 tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK ); // Temp Kelvin 72 float tempC = tempK - 273.15; // Convert Kelvin to Celcius 73 float tempF = (tempC * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit 74 /* replaced 75 float tempVolts = tempReading * 5.0 / 1024.0; 76 float tempC = (tempVolts - 0.5) * 10.0; 77 float tempF = tempC * 9.0 / 5.0 + 32.0; 78 */ 79 // Display Temperature in C or F 80 lcd.setCursor(0, 0); 81 //lcd.print("Temp C "); 82 // Display Temperature in F 83 lcd.print("Temp F "); 84 lcd.setCursor(6, 0); 85 // Display Temperature in C 86 //lcd.print(tempC); 87 // Display Temperature in F 88 lcd.print(tempF); // prints temperature to LCD screen 89 delay(30000); 90 91 // the LED is off turn if the distance is greater than 10cm 92 if (distance<= 10) { // Checks the distance 93 ledState = HIGH; //Set LED to on if less than 10 cm 94 } else { 95 ledState = LOW; //Set LED to off 96 } 97 98 // set the LED with the ledState of the variable: 99 digitalWrite(ledPin, ledState); 100}
Temperature and Distance Monitoring
arduino
1/* 2 Temperature and Distance Monitoring 3 Measures the distance and temperature every 30 secs, and turn on the red LED when the distance is less than 10 cm. 4 You will need to download the NewPing and Liquid Crystal libary from libary manager. 5 */ 6// Unltra Sonice test 7#include <NewPing.h> //Libary used for the ultra sonic 8#include <LiquidCrystal.h> //Libary used for the LCD screen 9//Define Constants 10#define echoPin 4 // attach pin D4 Arduino to pin Echo of HC-SR04 11#define trigPin 3 // attach pin D3 Arduino to pin Trigger of HC-SR04 12#define Max_DISTANCE 400 // Set the max distance for the Ultra sonic 13#define ledPin 2 // LED pin D2 14NewPing sonar(trigPin,echoPin, Max_DISTANCE); 15 16//Define Variables, Variables will change as the program is ran: 17float duration, distance; 18int iterations = 10; 19int tempPin = 0; 20int ledState = LOW; // ledState used to set the LED at the start of the program 21// BS E D4 D5 D6 D7 22LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Pins for the LCD screen. These can be changed 23 24void setup() 25{ 26 //Set Pins and serial port printout 27 Serial.begin(9600); // Serial Communication is starting with 9600 of baudrate speed 28 Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor 29 Serial.println("with Arduino UNO R3"); 30 // Set the row and columns for the LCD screen 31 lcd.begin(16, 1); // Set column for distance display 32 lcd.begin(16, 2); // Set column for Temp display 33 34 // set the digital pin as output: 35 pinMode(ledPin, OUTPUT); 36} 37 38// Main Program Loop this will run until program is halted 39void loop() 40{ 41 //Begin Ultra Sonic Code and print to LCD screen 42duration = sonar.ping_median(iterations); 43distance = (duration / 2) * 0.0343; 44 45// Displays the Output 46 lcd.setCursor(0, 1); // set the column on the LCD 47 lcd.print ("Dist: cm"); // Displays the distance on the LCD 48 49 /* Checks that the distance is greater than or equal to 400 cm, 50 or if the distance is less than or equal to 5 cm 51 */ 52 if (distance>= 400 || distance<= 5) { 53 lcd.setCursor(0, 1); 54 lcd.print("Out of range"); // Displays the out of range on the LCD 55 } 56 else { 57 lcd.setCursor(5, 1); // set the column on the LCD 58 lcd.print (distance); // Displays the distance on the LCD 59 } 60// Serial monitor display this is mainly for debugging 61 Serial.print(" Duration: "); // Displays the distance on the Serial Monitor 62 Serial.print(duration); // Displays the distance on the Serial Monitor 63 Serial.print(" Dist: "); // Displays the distance on the Serial Monitor 64 Serial.print(distance); // Displays the distance on the Serial Monitor 65 Serial.println(" cm"); // Displays the distance on the Serial Monitor 66 67 // TEMP monitoring code 68 int tempReading = analogRead(tempPin); 69 // Temp conversion formula 70 double tempK = log(10000.0 * ((1024.0 / tempReading - 1))); 71 tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK ); // Temp Kelvin 72 float tempC = tempK - 273.15; // Convert Kelvin to Celcius 73 float tempF = (tempC * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit 74 /* replaced 75 float tempVolts = tempReading * 5.0 / 1024.0; 76 float tempC = (tempVolts - 0.5) * 10.0; 77 float tempF = tempC * 9.0 / 5.0 + 32.0; 78 */ 79 // Display Temperature in C or F 80 lcd.setCursor(0, 0); 81 //lcd.print("Temp C "); 82 // Display Temperature in F 83 lcd.print("Temp F "); 84 lcd.setCursor(6, 0); 85 // Display Temperature in C 86 //lcd.print(tempC); 87 // Display Temperature in F 88 lcd.print(tempF); // prints temperature to LCD screen 89 delay(30000); 90 91 // the LED is off turn if the distance is greater than 10cm 92 if (distance<= 10) { // Checks the distance 93 ledState = HIGH; //Set LED to on if less than 10 cm 94 } else { 95 ledState = LOW; //Set LED to off 96 } 97 98 // set the LED with the ledState of the variable: 99 digitalWrite(ledPin, ledState); 100}
Downloadable files
Schematics
Schematics
circuit diagrams
circuit diagrams
Schematics
Schematics
Comments
Only logged in users can leave comments