Very simple circuit, but I am getting some strange behavior when using external power.
I want to make a DC motor spin when an IR proximity sensor reading goes above 200.
I am using a sharp 2Y0A21 IR sensor connected to Arduino analog pin0.
I have a DC motor connected to digital pin2.
The system works fine when connected to my Mac via USB, but when I use an external power source (9v or 6v battery) the motor will spin even with nothing close to the IR sensor.
Code:
int motorPin = 2; // digital out pin for the DC motor
int sensorValue;
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(A0); // range 0--1023
if(sensorValue > 200){
digitalWrite(motorPin, HIGH);
}
else{
digitalWrite(motorPin, LOW);
}
delay(50);
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
Sorry, here is the circuit.
It works fine with power coming through USB from pc, but with the external 9v battery the motor will turn on even with nothing in front of the ir sensor.
When you plug the Uno into the PC, do you power the motor from the Uno or still from the battery? I don't see anything that looks out of place, so I'm just wondering exactly what changes between the two set ups.
Can you post the specs for the motor? My first guess is its too much power demand for the supply.
Especially as you have no capacitor for supply decoupling.
try adding
also - just an idea - try setting a sensible data rate eg 57600.
Finally - can the Arduino provide enough current to drive the motor? If you place too much demand the digital pin will burn out.
Since USB can usually supply an amp or more, can probably run a small motor though it is NOT wise to drive a motor from an Arduino pin as the current rating is limited. Also a small 9v battery doesn't have much capability to drive a motor. You should at least use a MOSFET to buffer the digital output, and if you want to use a battery have a bigger one more suited to the current needed by the motor. With a MOSFET, you could directly switch the battery to the motor.
Thanks, I tried adding the line of code in the setup but no luck.
I don' think it is a problem with the motor itself as I get the same problem if I replace the motor on pin2 with an LED: it lights up but wont go out.
It's like when I use the external battery, the pin remains high and wont go low, regardless of the analog input.
it's very confusing....!
In the data sheet of a sharp 2Y0A21 IR sensor, output terminal voltage of absolute maximum rating is depended on power voltage. gp2y0a21yk_e.pdf (akizukidenshi.com)
So the sensor is probably outputting 1.8 times more voltage than when driven by a PC.
If you want to make same condition, you shoud connect the sensor power line to Uno 5V pin.
Thank you for the idea!
Sadly, connecting the sensor to the 5V doesn't solve the problem. The motor still spins regardless. I also tried regulating the power to the IR sensor using various resistors. No good.
It seems like the motor is sucking power from the Arduino pin.
It is very confusing.
A motor, or a motor driver.
You can't of course connect a motor to a pin with a current limit of 20mA (40mA absolute max). Motors take several hundred milliamps for a tiny one, and several Amps for a small one.
Connecting a motor directly to a pin will sooner or later burn out the pin, or the whole processor.
Powering a motor from the Arduino supply is equally foolish.
Use a motor driver with a separate motor supply, with only the grounds shared.
Leo..