TMP36+Servo

Hello.

I’m just learning in arduino, I’m working in tinkerkad, I’m studying here, in general, you need to make a program so that the position of the servo motor shaft changes depending on the data that is measured by the temperature sensor

Minimum Temperature: 20

Servo position at min temperature: 30

Maximum Temperature: 70

Servo position at max temperature: 110

Here I tried to do this:

#include <Servo.h>
 
Servo s1;

const int TMP36 = A0;
float temperature;
const int MinTemp=40;
const int MaxTemp=90;
 
void setup()
{
  s1.attach(6);
  Serial.begin(9600);
  pinMode(6, OUTPUT);
}
 
 
void loop()
{
  temperature=analogRead(0);         
  if(temperature = MinTemp){
  s1.write(50);
  }
  if(temperature = MaxTemp){
  s1.write(130);
  }
}

You haven't said what problems you have, if any. But I'd start in your if statements by changing the = (assign) to == (compare).

But even then your code will move the servo to 50 when the temp is exactly the minimum then it will stay there until the temp reaches the max. Is that really what you want it to do?

Steve

slipstick:
You haven't said what problems you have, if any. But I'd start in your if statements by changing the = (assign) to == (compare).

But even then your code will move the servo to 50 when the temp is exactly the minimum then it will stay there until the temp reaches the max. Is that really what you want it to do?

Steve

Yes, maybe == will help)

Don’t confuse the assignment operator = (single equal sign) with the equal comparison operator == (double equal signs), which evaluates whether two expressions are equal.