Devices & Components
Arduino Uno Rev3
RGB Diffused Common Cathode
Breadboard (generic)
Resistor 220 ohm
Temperature Sensor
Jumper wires (generic)
SG90 Micro-servo motor
Project description
Code
Code...
c_cpp
1#define tempPin A0 // temperature sensor 2#define red 11 // RGB led red 3#define green 12 // RGB led green 4#include <Servo.h> // Servo libary 5Servo myservo; // to control the servo motor 6int pos = 0; // position starting on 0 7 8 9void setup() { 10 Serial.begin(9600); 11 pinMode(red, OUTPUT); 12 pinMode(green, OUTPUT); 13 myservo.attach(3); // servo on pin 3 14} 15 16void loop() { 17 float value = analogRead(tempPin); 18 float tempr = (value * 500) / 1024; // calc of temperature in celcius 19 Serial.print("Temperature: "); 20 Serial.print(tempr); 21 Serial.print("*C"); 22 Serial.println(); // it'll show in the serial monitor 23 24 if (tempr <= 40){ // less than 40 25 digitalWrite(red, LOW); 26 digitalWrite(green, HIGH); 27 myservo.detach(); // stops the servo motor 28 29 } 30 else if (tempr >= 40){ // greater than 40 31 digitalWrite(red, HIGH); 32 digitalWrite(green, LOW); 33 myservo.attach(3); 34 myservo.write(pos); // position for the servo to turn 35 } 36 delay(200); 37 38 for (pos = 0; pos <= 180; pos += 1){ // go forward 180 degrees 39 myservo.write(pos); 40 delay(20); 41 } 42 for (pos = 180; pos <= 0; pos -= 1) { // go backward 180 degrees 43 myservo.write(pos); 44 delay(20); 45 } 46} // loop
Code...
c_cpp
1#define tempPin A0 // temperature sensor 2#define red 11 // RGB led red 3#define green 12 // RGB led green 4#include <Servo.h> // Servo libary 5Servo myservo; // to control the servo motor 6int pos = 0; // position starting on 0 7 8 9void setup() { 10 Serial.begin(9600); 11 pinMode(red, OUTPUT); 12 pinMode(green, OUTPUT); 13 myservo.attach(3); // servo on pin 3 14} 15 16void loop() { 17 float value = analogRead(tempPin); 18 float tempr = (value * 500) / 1024; // calc of temperature in celcius 19 Serial.print("Temperature: "); 20 Serial.print(tempr); 21 Serial.print("*C"); 22 Serial.println(); // it'll show in the serial monitor 23 24 if (tempr <= 40){ // less than 40 25 digitalWrite(red, LOW); 26 digitalWrite(green, HIGH); 27 myservo.detach(); // stops the servo motor 28 29 } 30 else if (tempr >= 40){ // greater than 40 31 digitalWrite(red, HIGH); 32 digitalWrite(green, LOW); 33 myservo.attach(3); 34 myservo.write(pos); // position for the servo to turn 35 } 36 delay(200); 37 38 for (pos = 0; pos <= 180; pos += 1){ // go forward 180 degrees 39 myservo.write(pos); 40 delay(20); 41 } 42 for (pos = 180; pos <= 0; pos -= 1) { // go backward 180 degrees 43 myservo.write(pos); 44 delay(20); 45 } 46} // loop
Downloadable files
temperature sensor using the servo and RGB led
temperature sensor using the servo and RGB led

Comments
Only logged in users can leave comments