Im trying to attach two sensors and two motors together. Motors speed up when temperature is less than 50 and light lights up. I need the code for it. Im just a beginner I dont know how its done. please help me
Temperature sensor: LM35
Please reply asap
I’m assuming that you want this in Fahrenheit correct.
So, I found a guy on youtube who is basically doing the same exact thing. The cool part is that he is explining the code and everything. Next time you’ll be able to do it yourself. Here’s the video: https://youtu.be/_fNto4JOu4E
Here is the code he used:
Here is the code that he used in his video
#include <Servo.h> // Servo header file is needed to control the servo motor.
Servo servo1; // Create servo object to allow us to control the servo motor.
float tempC; // create variable to store the temperature in.
int tempPin = 0; // Attach vout to analog pin 0.
int led = 13; // attach led to pin 13.
int fan1 = 3; // attach base of transistor to digital pin 3.
int pos = 0; // create variable to store the position of the servo motor.
void setup() // Will execute once at the start of the code.
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode (led, OUTPUT); // sets the led pin 13 up as an output.
pinMode (fan1, OUTPUT); // sets the fan1 pin 3 up as an output.
servo1.attach(5); // attaches the servo motor to digital pin 5.
}
void loop() // code here will continue to replay nutil powered off.
{
tempC = analogRead(tempPin); // read the analog value from the lm35 sensor.
tempC = (5.0 * tempC * 100.0)/1024.0; // convert the analog input to temperature in centigrade.
Serial.print((byte)tempC); // send the data to the computer.
if (tempC > 24) // creates bool expression for analyzation. if it evaluates to true,
{ // the body of the if statement will execute.
pos = 180; // sets pos equal to 180.
digitalWrite (led, HIGH); // turns on led.
digitalWrite (fan1, HIGH); // turns on fan1.
servo1.write(pos); // turns servo to 180 degrees.
}
else // if the if equation evaluates to false the else statement will execute.
{
pos = 90; // sets pos equal to 90.
digitalWrite (led, LOW); // turns off led.
digitalWrite (fan1, LOW); // turns off fan1.
servo1.write(pos); // turns servo to 90 degrees.
}
delay(3000); // wait 3 seconds before redoing the loop.
}
Processing Code:
//import Serial communication library
import processing.serial.*;
//init variables
Serial commPort;
float tempC;
float tempF;
int yDist;
PFont font12;
PFont font24;
float[] tempHistory = new float[100];
void setup()
{
//setup fonts for use throughout the application
font12 = loadFont(“Verdana-12.vlw”);
font24 = loadFont(“Verdana-24.vlw”);
//set the size of the window
size(210, 200);
//init serial communication port
commPort = new Serial(this, “COM5”, 9600);
//fill tempHistory with default temps
for(int index = 0; index<100; index++)
tempHistory[index] = 0;
}
void draw()
{
//get the temp from the serial port
while (commPort.available() > 0)
{
tempC = commPort.read();
//refresh the background to clear old data
background(123);
//draw the temp rectangle
colorMode(RGB, 160); //use color mode sized for fading
stroke (0);
rect (49,19,22,162);
//fade red and blue within the rectangle
for (int colorIndex = 0; colorIndex <= 160; colorIndex++)
{
stroke(160 - colorIndex, 0, colorIndex);
line(50, colorIndex + 20, 70, colorIndex + 20);
}
//draw graph
stroke(0);
fill(255,255,255);
rect(90,80,100,100);
for (int index = 0; index<100; index++)
{
if(index == 99)
tempHistory[index] = tempC;
else
tempHistory[index] = tempHistory[index + 1];
point(90 + index, 180 - tempHistory[index]);
}
//write reference values
fill(0,0,0);
textFont(font12);
textAlign(RIGHT);
text("212 F", 45, 25);
text("32 F", 45, 187);
//draw triangle pointer
yDist = int(160 - (160 * (tempC * 0.01)));
stroke(0);
triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
//write the temp in C and F
fill(0,0,0);
textFont(font24);
textAlign(LEFT);
tempC = 5*tempC*100/1024;
text(str(int(tempC)) + " C", 115, 37);
text(str(int(tempC*1.8+32)) + " F", 115, 65);
}
}
I’m using a motor instead of the fan but its not working. Im not sure what to change in my code to get it to work or where to connect the wires. Can you please help?
Hey,
Here’s a link to the diagram containing the components you asked for - https://www.circuito.io/app?components=9442,9590,11021,333429,7654321
It consists of 2 DC motors, LM35 and an LED that serves as the light.
After you connect your circuit, test it using the circuito.io test code.
In order to write the code properly for your project, you need to think how you want it to behave in different situations, for example - what happens in idle mode? What happens when the temperature is above 50 degrees? Speed up the motors from zero or from a constant speed? These are just example questions of the scenarios that you need to first think about before you start writing the code.
Feel free to share with us your thoughts and we will try to help
Cheers,
Anat
Hey, can you post the code so i can see whats wrong?
dear shamsa,
can you send the code , and your circuit.
and did you use arduino ??
float tempC;
int tempPin = 0;
int ledPin = 13; //connected to pin13
int motor1 = 5;//connected to pin 5
//setup function
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(motor1, OUTPUT);
}
void loop()
{
tempC = analogRead(tempPin);
tempC = (5.0tempC100.01)/1024.0;
Serial.println((byte)tempC);
if (tempC > 22)
{
digitalWrite(ledPin, HIGH);
digitalWrite(motor1, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(motor1, LOW);
}
delay(3000);
}
yes I used Arduino I connected it the way the guy in the video connected his but I’ve had trouble with the fan since instead of using a fan I’m using a motor. this is the code I’ve used so far:
float tempC;
int tempPin = 0;
int ledPin = 13; //connected to pin13
int motor1 = 5;//connected to pin 5
//setup function
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(motor1, OUTPUT);
}
void loop()
{
tempC = analogRead(tempPin);
tempC = (5.0tempC100.01)/1024.0;
Serial.println((byte)tempC);
if (tempC > 22)
{
digitalWrite(ledPin, HIGH);
digitalWrite(motor1, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(motor1, LOW);
}
delay(3000);
}
Dear Shamsa,
can you try this code and send the Serial Print result to try to fix it together:
//////////////////////////////////////////
float tempC;
float sum=0,avg=0;
int tempPin = A0;
int ledPin = 13; //connected to pin13
int motor1 = 5;//connected to pin 5
//setup function
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(motor1, OUTPUT);
}
void loop()
{
sum=0;
for(int i=0;i<10;i++){
tempC = analogRead(tempPin);
tempC = (5.0tempC100.01)/1024.0;
sum+=tempC;
}
avg=sum/10.0;
Serial.println(avg);
if (avg > 22)
{
digitalWrite(ledPin, HIGH);
digitalWrite(motor1, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(motor1, LOW);
}
delay(3000);
}
/////////////////////////////////////////////////
error: unable to find numeric literal operator ‘operator""tempC100.01’
tempC = (5.0tempC100.01)/1024.0;
The motor still doesn’t work
use this :
tempC = (5.0tempC100.01)/1024.0;
you must but " * "
after 5.0 and tempC
if the motor still not working .
read the value from the the sensor.
in my code the monitor will print the avg, its the value form sensor please let me see what the value is it ?
It prints the temperature only and lights up the LED.
you don’t have any issue about temperature.
and the led working when the temperature reach the the level you but it,
but the motor not working, then you don’t have any issue in your code, you have a problem in the hardware,
kindly send a photo for your circuit ,
and which motor you are using?
I have been having trouble getting the motor to work because of the placement of it on the Arduino. I have used this video https://www.youtube.com/watch?v=_fNto4JOu4E&feature=youtu.be but I have stopped until he attached the fan. Can you help me with the placement of the motor? Im using a DC motor.