Need help with code

New to Arduino and coding
trying to make a temperature controlled fan using lm35 as the sensor
Screenshot 2024-05-21 140118
lm35 temperature sensor reading is wrong and i think the problem is with my code ( I used Chatgpt to write it and edited it to try and fix it )

// Define the pin for the temperature sensor
const int tempSensorPin = A0;

// Define the pin for the fan
const int fanPin = 9; // Change this to the appropriate PWM pin for your setup

// Variables to store temperature and fan speed
float temperature;
int fanSpeed;

// Define temperature thresholds
const float roomTemp = 25.0; // Room temperature threshold
const float maxTemp = 30.0;  // Maximum temperature threshold

// Define fan speed thresholds
const int minFanSpeed = 50; // Minimum fan speed (slow)
const int maxFanSpeed = 255; // Maximum fan speed (fast)

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize the fan pin as an output
  pinMode(fanPin, OUTPUT);
}

void loop() {
 
     //voltage reading from the temperature sensor
 int reading = analogRead(tempSensorPin);  
 
 // converting that reading to voltage then to degrees
 float temp = (reading / 4095.0)*3300.0;//raw value to voltage
 temp = temp / 10; //voltage to celsius

  // Adjust fan speed based on temperature
  if (temp <= roomTemp) {
    // Fan speed is slow when temperature is at or below room temperature
    fanSpeed = map(temp, 0, roomTemp, maxFanSpeed, minFanSpeed);
  } else if (temp > roomTemp && temperature <= maxTemp) {
    // Fan speed increases linearly within the defined temperature range
    fanSpeed = map(temp, roomTemp, maxTemp, minFanSpeed, maxFanSpeed);
  } else {
    // Fan runs at maximum speed if temperature exceeds maxTemp
    fanSpeed = maxFanSpeed;
  }

  // Set the fan speed
  analogWrite(fanPin, fanSpeed);

  // Print  fan speed to serial monitor
  Serial.print(" Fan Speed: ");
  Serial.println(fanSpeed);
   Serial.print("Temperature ");Serial.print(temp); Serial.println(" °C");

  delay(1000); // delay time
}



Which arduino are you using?

Arduino uno

This tutorial explains how to connect the LM35 and how to read the temperature.

so from what i understand the formula i used to convert the reading to temp was wrong ,
tried to fix now it alternates between displaying unstable temperature for a few seconds and returning to 0 for another few seconds . and the fan speed is suddenly exceeding max speed

// Define the pin for the temperature sensor
const int SensorPin = A0;

// Define the pin for the fan
const int fanPin = 9; // Change this to the appropriate PWM pin for your setup

// Variables to store temperature and fan speed
float temperature;
int fanSpeed;

// Define temperature thresholds
const float roomTemp = 25.0; // Room temperature threshold
const float maxTemp = 30.0;  // Maximum temperature threshold

// Define fan speed thresholds
const int minFanSpeed = 50; // Minimum fan speed (slow)
const int maxFanSpeed = 255; // Maximum fan speed (fast)

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize the fan pin as an output
  pinMode(fanPin, OUTPUT);
}

void loop() {
  
 
 // Get the voltage reading from the LM35
  int reading = analogRead(SensorPin);

  // Convert that reading into voltage
  float voltage = reading * (5.0 / 1024.0);

  // Convert the voltage into the temperature in Celsius
  float temperatureC = voltage * 100;


  // Adjust fan speed based on temperature
  if (temperatureC <= roomTemp) {
    // Fan speed is slow when temperature is at or below room temperature
    fanSpeed = map(temperatureC, 0, roomTemp, maxFanSpeed, minFanSpeed);
  } else if (temperatureC > roomTemp && temperature <= maxTemp) {
    // Fan speed increases linearly within the defined temperature range
    fanSpeed = map(temperatureC, roomTemp, maxTemp, minFanSpeed, maxFanSpeed);
  } else {
    // Fan runs at maximum speed if temperature exceeds maxTemp
    fanSpeed = maxFanSpeed;
  }

  // Set the fan speed
  analogWrite(fanPin, fanSpeed);

  // Print  fan speed to serial monitor
  Serial.print(" Fan Speed: ");
  Serial.println(fanSpeed);
   Serial.print("Temperature ");Serial.print(temperatureC); Serial.println(" °C");

  delay(1000); // delay time
}


Check your connections.
Sometimes breadboard connections can be loose or be internment.
Make sure the connections are tight.

checked the connections they're all tight , tried to rewire on another breadboard the issue stayed the same .

There have been quite a few fake LM35 on the market.
Where did you buy them and what was the cost?

Try the code without the fan connected, see if its stable

tried a different sensor and it works now thanks

Guess the other one was bad.
Glad you got it to work
Have a nice daty

1 Like

This is code for a 3.3volt processor with a 12-bit A/D.
So the code is not compatible with an Uno R3.
Leo..

@Wawa See post #5.

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