Arduino mlx90614 and servo motor

Hello,
I'm kind of new to Arduino, and I'm trying to do his project where I have an mlx90614 thermal sensor to detect high heat levels. When the high heat level is detected, the servo motor will move. My code has no errors and the sensor is working fine. However, the servo motor is constantly moving regardless of the if statement that I put in the code. If anyone has any suggestions please let me know.

#include <Servo.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
Servo myservo;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup()
{
Serial.begin(9600);
myservo.attach(2,600,2300);
Serial.println("Adafruit MLX90614 test");
mlx.begin();
}
void loop()
{
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempF());
Serial.print("*F\tObject = "); Serial.print(mlx.readObjectTempF()); Serial.println("*F");

Serial.println();
delay(500);
{
if(mlx.readAmbientTempC()<=100)

{
//delay(1000); // update sensor reading each one second
//digitalWrite(2,HIGH);
myservo.write(0);
delay(1000);
myservo.write(90);
}
else
{
//digitalWrite(2,LOW);
myservo.write(90);
delay(1000);
myservo.write(0);

}
}
}

What type of servo ?
Is it, by any chance, a "continuous rotation" servo, so not really a servo at all

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

It is a SG90 micro controller servo. As for the code.

#include <Servo.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
  Servo myservo;
  Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup() 
{
  Serial.begin(9600);
  myservo.attach(2,600,2300);
  Serial.println("Adafruit MLX90614 test");  
  mlx.begin();  
}
void loop() 
{
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC()); 
  Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempF()); 
  Serial.print("*F\tObject = "); Serial.print(mlx.readObjectTempF()); Serial.println("*F");
  
  Serial.println();
  delay(500);
{
  if(mlx.readAmbientTempC()<=100)

  {
   //delay(1000); // update sensor reading each one second
//digitalWrite(2,HIGH);
    myservo.write(0);
    delay(1000);
      myservo.write(90);
  }
  else
  {
    //digitalWrite(2,LOW);
    myservo.write(90);
    delay(1000);
    myservo.write(0);
      
  }
}
}

Thanks for post the code in code tags. I hope that you can see how much easier it is to read and copy

As to your problem, what exactly does the servo do when you run the program ?
What do you see in the Serial monitor ?

The servo just moves constantly back and forth when I run the code.

And the Serial output ?

I also see the temperature readings in the serial monitor.

achyutr2007:
I also see the temperature readings in the serial monitor.

OK, but are they, particularly readObjectTempC, sensible and relatively stable ?

Yes

You said earlier

The servo just moves constantly back and forth when I run the code.

Look at your code and think what happens if the temperature is less than 100

The servo will move to 0, wait 1 second then move to 90. loop() will then end, the temperatures will be displayed and a delay of 500 milliseconds occurs

Then the servo will move to 0, wait 1 second then move to 90. loop() will then end, the temperatures will be displayed and a delay of 500 milliseconds occurs

This is what you are seeing. What do you want the servo to do if the temperature is less than 100 ?

I want my servo to remain still when it's less than 100 degrees. I tried flipping the less than sign, and the servo still movies. My goal is to have the servo move one rotation when it's above 100 degrees, but when it's less than 100 degrees, I want it to remain still.

I want my servo to remain still when it's less than 100 degrees.

Then move it to say 90 and don't move it back to 0

As a matter of interest, how does the servo behave if you run the Servo Sweep example ?

How would I say don't move for the servo? I am kinda new to arduino, sorry.

You command a servo not to move by simply writing a value to it. If your SG90 rotates continuously when you write a value of say 0 to it then you don't have a servo at all you have a "continuous rotation servo", ie not really a servo at all, rather an electronically controlled motor. Not the same thing at all. See my question in reply #1

If that is the case then all you can do is to control its speed and direction, not its position. Writing 0 to it will rotate it at full speed in one direction whilst writing 180 to it will cause it to rotate at full speed in the opposite direction. Writing 90, or something close to 90, to it may cause it to stop rotating but you cannot control its position

Run the Sweep example and report what happens

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.