Skip to content

Instantly share code, notes, and snippets.

@dev-kperera
Last active March 23, 2024 15:39
Show Gist options
  • Save dev-kperera/67792cf1125d1544bfcf27ff2f8c6fbd to your computer and use it in GitHub Desktop.
Save dev-kperera/67792cf1125d1544bfcf27ff2f8c6fbd to your computer and use it in GitHub Desktop.
Using a Temperature Sensor to Control the Speed of a Motor Using Arduino
float temp;
int tempPin = A1; //arduino pin used for temperature sensor. CHANGE THIS BASED ON YOUR CONFIGURATION
int tempMin = 70;
int tempMax = 75;
int motor = 2; // the pin where motor is connected. CHANGE THIS BASED ON YOUR CONFIGURATION
int motorSpeed = 0;
void setup() {
pinMode(motor, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin);
temp = (temp *5.0*100.0)/1024.0;
Serial.println(temp);
delay(1000);
if(temp < tempMin) {
motorSpeed = 0;
digitalWrite(motor, LOW);
}
if((temp >= tempMin) && (temp <= tempMax))
{
motorSpeed = map(temp, tempMin, tempMax, 32, 255);
analogWrite(motor, motorSpeed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment