Motor Driver Interfering with Temperature Sensor

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

The 5V output of the Arduino cannot be used to power motors, servos, valves, solenoids, etc. The motor is overloading the on board 5V regulator or the USB power connection and will cause sensors and the Arduino to malfunction or fail completely.

Use a separate motor power supply and don’t forget to connect the grounds. Servo example:

Totally agree with jremington. I’ve had similar issues using the TMP3x temperature sensors myself and they come from voltage instability on the 5 volt line.

A separate power supply for the motor will likely solve your problem and is highly recommended for overall stability of your project. If you continue to have problems after incorporating a separate power supply you might try a digital temperature sensor rather than an analog sensor. A DS18B20 temperature sensor would work well and only needs 1 I/O pin like your current sensor. Another digital temperature option would be one of the Qwiic enabled sensors like the STTS22H.

1 Like

I tried a power supply module and put it at 5V, problem is the fan runs a lot slower when we need it to run faster to produce air. I also hacked a toy fan from the dollar store and was able to get it to work because it has a stronger motor. Again, the same problem occurs where the temperature reading gets messed up or the fan speed is too slow at 5V (even the dollar store one).

Do you have any recommendations for power supplies? The dollar store fan simply has 2 wire connections for positive and negative.

Use a higher voltage power supply for the motor only and connect it to IC pins 4/5 and 8.

Also, double check your connections to the L293D, they don’t look right to me.

You can put two 3.7V lipo batteries in series to supply power to your circuit. I used this combination in many of my car robot projects. And in case you need the proteus library of L298, you can get it from here: L298 Motor Driver Library for Proteus - The Engineering Projects