I have a motor connected directly to Pin 4 (digital) on an Arduino Uno. The motor goes when motion is detected. I understand I should have the motor hooked up to a transistor circuit instead so I can power it with more than the 5V that an Arduino can give me, but for what I'm doing 5V is sufficient. However, when the motion is detected, sometimes it outputs 2V and sometimes it outputs 0.5V (I'm measuring this directly with a voltage meter across the motor). I need at least 3V. Is there a way to make the Uno output at least 3V every time? 5V would be even better.
Basically, right now I just have an if statement that makes the motor go if motion is detected. I just simply have a digitalWrite(motorpin, HIGH). However, its inconsistent and outputs too little voltage...
// Motion Control Motor
// Button Variables
int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
//Motor Variables
int motorPin = 4;
//Sensor Variables
int sensePin = 1;
// Program Setup
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(sensePin,INPUT);
Serial.begin(9600);
}
// Button Debounce Function
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
// Reset Function
void reset(){
lastButton = LOW;
currentButton = LOW;
ledOn = false;
digitalWrite(ledPin, ledOn);
}
// Program Loop
void loop()
{
Serial.println(analogRead(sensePin));
delay(100);
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH){
ledOn = !ledOn;
lastButton = currentButton;
digitalWrite(ledPin, ledOn);
while(true){
Serial.println(analogRead(sensePin));
delay(100);
if (analogRead(sensePin) > 80){
digitalWrite(motorPin, HIGH);
delay(1000);
digitalWrite(motorPin, LOW);
//for(int i=255; i>=0; i--){
//analogWrite(motorPin, i);
//delay(10);
//}
reset();
break;
}
else{
delay(100);
}
}
}
}
My electrical engineering knowledge isn't very vast, but it seems like there is a simple solution that I am missing. Any help would be greatly appreciated. Thanks!