I read that I can setup analog inputs as digital, but I seem to be having an issue. I use digitalRead to read the four inputs of an L4 RF Latching Receiver on A0, A1, A2, and A3, but the A0 input always seems to stay HIGH, resulting in two HIGH inputs. Since the receiver only puts one pin high at a time, and it seemed to work fine using digital inputs, perhaps the issue is related to the digital interpretation of the analog input? I haven't been able to fix it.
By the way, the reason I'm using the analog inputs is because I'm controlling a motor via an Adafruit Motor Shield stacked onto an Arduino Uno R3 and the Motor Shield AFMotor library requires all of the digital pins for operation. (Setting the L4 up on the Motor Shield's digital pins worked, but the motor wouldn't run...) So, I'm using the A0-A3 analog inputs and the +5 and GND signals from the Motor Shield through-hole region.
Arduino Reference for DigitalRead - http://arduino.cc/en/Reference/DigitalRead
Any ideas what I might be doing incorrectly?
Thank you.
Digital reading of analog inputs
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Digital reading of analog inputs
The analog pins act no differently than digital pins when used with digitalRead.
Post the code you are using and a photo of your connections and we might be able to spot the problem.
Post the code you are using and a photo of your connections and we might be able to spot the problem.
- raidoh
- Posts: 9
- Joined: Mon Oct 08, 2012 9:59 pm
Re: Digital reading of analog inputs
Thanks for your quick response. Here's my code and a photo of the setup. I'd commented out Pin A0 and revised the if statement so I could continue to work...
Code: Select all
// Control a Stepper motor with a wireless push-button remote through the Adafruit Motor Shield
#include <AFMotor.h>
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #1 (M1 and M2)
AF_Stepper motor(200, 1);
// Setup Pins
const int buttonA = A0; // the pin that the pushbutton is attached to
const int buttonB = A1;
const int buttonC = A2;
const int buttonD = A3;
// Variables will change:
int buttonStateA = 0; // current state of the button
int buttonStateB = 0; // current state of the button
int buttonStateC = 0; // current state of the button
int buttonStateD = 0; // current state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
pinMode(buttonD, INPUT);
// initialize serial communication:
Serial.begin(9600);
motor.setSpeed(0); // Start with the motor off (0 RPM)
}
void loop() {
// read the pushbutton input pin:
buttonStateA = digitalRead(buttonA);
buttonStateB = digitalRead(buttonB);
buttonStateC = digitalRead(buttonC);
buttonStateD = digitalRead(buttonD);
Serial.println(buttonStateA);
Serial.println(buttonStateB);
Serial.println(buttonStateC);
Serial.println(buttonStateD);
// determine which button has been pressed
/* if (buttonStateA == HIGH) {
Serial.println("A - High - Off");
motor.step(0, FORWARD, MICROSTEP); // Turn off the motor
}
*/
if (buttonStateB == HIGH) {
Serial.println("B - HIGH - 10RPM");
motor.setSpeed(10); // Set to 10 RPM
motor.step(10, FORWARD, MICROSTEP);
}
else if (buttonStateC == HIGH) {
Serial.println("C - HIGH - 20RPM");
motor.setSpeed(20); // Set to 20 RPM
motor.step(10, FORWARD, MICROSTEP);
}
else if (buttonStateD == HIGH) {
Serial.println("D - HIGH - 80RPM");
motor.setSpeed(80); // Set to 80 RPM
motor.step(10, FORWARD, MICROSTEP); // Move forward
}
else {
Serial.println("None - Off");
motor.step(0, FORWARD, MICROSTEP); // turn it off
}
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Digital reading of analog inputs
Your code looks fine. The data sheet on the receiver chip only shows examples of driving leds. You might need a weak pulldown resistor when driving a high-impedence input like a GPIO pin. I'd try a 10K resistor from A0 to ground. If that cleans up A0, I'd put pulldowns on the rest of them too.
- raidoh
- Posts: 9
- Joined: Mon Oct 08, 2012 9:59 pm
Re: Digital reading of analog inputs
Thanks for the suggestion. I tried the 10k pull-up resistor (by splicing into the green wire connected to A0 and connecting the resistor to GND), but it didn't have an effect. I checked the voltage between A0 and GND and it stays at 3.1VDC no matter what the command is. The other channels go from 0.2 to 4.4VDC and register correctly. Does that sound like a bad channel on the L4 receiver?
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Digital reading of analog inputs
That does sound like a bad channel. Contact [email protected] with a link to this thread and ask for a replacement receiver.I checked the voltage between A0 and GND and it stays at 3.1VDC no matter what the command is.
Please be positive and constructive with your questions and comments.