I’m doing a personal project where I’m trying to get a fan to turn at a certain speed depending on the distance of the person. For example, if they are close, the fan would be slower and if they would be farther away the fan spins faster. I’m also trying to add a temperature sensor to read the current temperature and use a water value to release water.
I haven’t gotten the water valve yet, and I’ve been able to get the fan speed part working however I’m having trouble with the temperature sensor. Also, I’m using a motor driver in order to reverse the direction of the fan so that it runs counter clockwise to push air out.
For context, I’m using:
- SparkFun RedBoard Qwiic
 - Breadboard
 - TMP36 temperature sensor
 - Ultrasonic sensor
 - Sparkfun Motor Driver
 - Hobby Gearmotor
 
Here’s how the circuit looks like:
Here’s the code I’m trying:
const int DIR_A = 5;
const int DIR_B = 4;
const int PWM = 6;
// int celsius = 0;
// int temp = A0; 
int sensePin = A0; //This is the Arduino Pin that will read the sensor output
int sensorInput; //The variable we will use to store the sensor input
double temp; //The variable we will use to store temperature in degrees.
float distance;
float distInches;
const int echo = 2;
const int trig = 1;
int fanSpeed = 0;
void setup()
{
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
// pinMode(temp,INPUT);
pinMode(DIR_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
pinMode(PWM, OUTPUT);
Serial.begin(9600);
}
void loop()
{
fanFunction();
tempFunction();
}
void fanFunction(){
 // Trigger the sensor to start measurement
   digitalWrite(trig,LOW);
   delayMicroseconds(5);
   
   //Start Measurement
   digitalWrite(trig,HIGH);
   delayMicroseconds(10);
   digitalWrite(trig,LOW);
   
   // Acquire and convert to inches
   distance = pulseIn(echo,HIGH);
   distance = distance*0.0001657;
   distInches = distance*39.37;
   
   // print the value on the serial monitor
   Serial.print("Distance: ");
   Serial.print(distInches);
   Serial.println(" inches");
   //Brightness of LED light changes based on the distance
   if(distInches > 36){
     fanSpeed = 255;
     Serial.println("Speed 1");
   }
   else if(distInches >= 24 && distInches < 36){
     fanSpeed = 191;
     Serial.println("Speed 2");
   }
   else if(distInches >= 12 && distInches < 24){
     fanSpeed = 127;
     Serial.println("Speed 3");
   }
   else if(distInches >= 3 && distInches < 12){
     fanSpeed = 63;
     Serial.println("Speed 4");
   }
   else if(distInches < 3){
     fanSpeed = 0;
     Serial.println("Speed 5");
   }
   // Control the motor driver for fan speed
 digitalWrite(DIR_A, HIGH);
 digitalWrite(DIR_B, LOW);
 analogWrite(PWM, fanSpeed);
}
void tempFunction()
{
 sensorInput = analogRead(A0); //read the analog sensor and store it
 temp = (double)sensorInput / 1024; //find percentage of input reading
 temp = temp * 5; //multiply by 5V to get voltage
 temp = temp - 0.5; //Subtract the offset
 temp = temp * 100; //Convert to degrees
 Serial.print("Current Temperature: ");
 Serial.println(temp);
}
Whenever I try to run the code, I keep getting the temperature to be widely different. The value goes from 18 degrees all the way to 40 degrees Celsius within seconds which tells me something is clearly wrong.
Can I get some help with the temperature sensor? I feel like the motor driver is interfering with this


