I'm using an Arduino Uno with a v3 motor shield, a photoresistor, and a 12V DC motor. My aim is simply to have the motor turn on when an overhead light is turned on. I've tested my setup and code without the motor and motor shield and it works just fine. My problem is that when I attach the motor shield and reattach the pins, pin 0 for photoresistor and pin 2 for LED no longer work. In fact, no pins that I attach to the motor shield are picked up. It's as if the Arduino doesn't even register the pins as being inserted. The motor just runs on. I'm very confused and I've looked all over the internet for an answer to this. Any help is much appreciated.
Here is my code:
int LED = 2;
int LDR = 0;
int base;
int threshold = 100; //found to be a good value during testing
void setup() {
//Setup photoresistor
pinMode(LDR,INPUT);
base = analogRead(LDR); //Take baseline reading for photoresistor
//Setup Channel A
pinMode(12,OUTPUT); //Initiates Motor Channel A pin
pinMode(9,OUTPUT); //Initiates Motor Channel A pin
//Setup Other
pinMode(LED,OUTPUT);
}
void loop() {
int v = analogRead(LDR);
if ((base - v) > threshold) {
//Blink LED if light is off
digitalWrite(LED,HIGH);
delay(100);
digitalWrite(LED,LOW);
delay(100);
}
else {
//Operate motor if light is on
//forward @ full speed
digitalWrite(12,HIGH); //Establishes forward direction of Channel A
digitalWrite(9,LOW); //Disengage the Brake for Channel A
analogWrite(3,255); //255 is full speed
delay(500);
digitalWrite(9,HIGH); //Eengage the Brake for Channel A
delay(100);
//backward @ full speed
digitalWrite(12,LOW); //Establishes backward direction of Channel A
digitalWrite(9,LOW); //Disengage the Brake for Channel A
analogWrite(3,255); //Spins the motor on Channel A at half speed
delay(500);
digitalWrite(9,HIGH); //Eengage the Brake for Channel A
delay(100);
}
}
What is rating of your motor?? 2) How many channel you are using?? 3)just print the LDR value before going inside the loop via serial monitor.
4)delay you are giving very small . you can't see blink if delay is too short.
Example code you can try with motor connected.
void setup() {
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
}
void loop(){
//Motor A forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 123); //Spins the motor on Channel B at half speed
delay(3000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
delay(1000);
//Motor A forward @ full speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 123); //Spins the motor on Channel A at half speed
//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
delay(3000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
delay(1000);
}
Motor rating is 12V. 1 channel, I believe. Will that solve the problem I'm having though? The arduino doesn't even pick up the photoresistor. I've tried using various PIN numbers from 1-8 and I get no response. Any help on this matter?
As per me you should check Photoresistor , above link shows connecting, Check first you can read the values.
Always try to share ur code and screen shot of output you are getting serial monitor.
what is current rating of your motor???
The arduino doesn't even pick up the photoresistor. I've tried using various PIN numbers from 1-8 and I get no response
you should use analog pins, instead of digital.
can you share out put for below code
const int LDR=0; //connect output of LDR sensor to analog pin 0
void setup()
{
Serial.begin(9600);
}
void loop()
{
int v = analogRead(LDR);
Serial.print("voltage:");
Serial.println(v);
delay(1000);
}
AMPS-N. I am using analog pins for the photoresistor. The arduino picks up neither the analog pins nor the digital. I thought the headers on the motor shield were supposed to allow you to still use these?