I am new to microcontrollers and/or the arduino board. I have a duemilanove board and am trying to get it to read input from a speed and direction sensor that switches between low and high or 0.14V and 12V. The board doesnt seem to distinguish the difference in these values although i believe it should give a high or low value(1 or 0). Any help would be greatly appreciated
I hope you aren't really feeding 12 volts into an Arduino pin.
If you aren't, post the code that tries to read the pin.
If you are, never mind. Fixing the code won't help.
Well its 11.4V if that makes a difference, the spec said it could handle up to 12V. Thanks
Here is the code i tried without leds, the serial output is something like:
0
0
0
0
when nothing
0
0
1
1
1
0
0
0
1
1
1
when its 0.14V or 11.4V
const int directionPin = 6; // pin that the sensor is attached to
int directionValue = 0;
void setup()
{
pinMode(directionPin, INPUT);
Serial.begin(9600);
}
void loop()
{
int directionValue = digitalRead(directionPin);
Serial.println(directionValue, DEC);
}
I tried this code with leds and it stayed on whenever the wire is connected to the pin regardless of the voltage change
const int directionPin = 6; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
int directionValue = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(directionPin, INPUT);
Serial.begin(9600);
}
void loop() {
// while the direction wire is high
while (digitalRead(directionPin) == HIGH) {
ledon();
}
// direction wire low
digitalWrite(ledPin, LOW);
}
void ledon() {
// turn on the indicator LED
digitalWrite(ledPin, HIGH);
int directionValue = digitalRead(directionPin);
Serial.println(directionValue, DEC);
}
I'm confused...Are you feeding 11.4V into an Arduino pin? :o
the spec said it could handle up to 12V.
What spec would that be?
You can power an Arduino with an input voltage up to 20 volts, although a maximum of 12 is better. But the digitial and analog pin input maximum voltage is 5V, for a 5V board.
Is the ground for the sensor connected to the ground of the Arduino? It needs to be, for the circuit to be complete.
Although, I really don't recommend that you actually connect it, if it isn't already connected.
No the ground wasnt connected yet, i guess i misunderstood this:
Input Voltage (recommended) 7-12V
Input Voltage (limits) 6-20V
I read further and your right, they are meant to operate at 5V.
So how do i get a sensor that reads .14 and 11.4 V to work with the 5V inputs?
So i prob need to quit piggy backing of of the 12V system my sensor is wired through and connect it all to arduino board so then it will be on 5 volts and will return 5V signal on high instead of 12?
You use a voltage divider. A voltage divider consists of a pair of resistors connected in series. One end of the series goes to your sensor. The other end goes to ground. The middle point is connected to the digital pin:
sensor --- + --- ground
|
pin
The --- are the resistors. Since you are trying to drop 12 volts to 5 volts, you'd need about a 7K ohm resistor and a 5K ohm resistor. The larger one goes closer to the sensor. You can use any value resistor that maintains the 7:5 ratio, as long as the resulting current flow is below the rated current rating of the resistors. You can use more than one resistor, on each side, as long as the total resistance on each side maintains the 7:5 ratio. If you go higher that 7 or less than 5, that's OK, as long as you don't go too far.
K Thanks alot guys
It works perfect when all wired through the arduino, on off on off perfect, im so excited, thanks so much ive been fighting this thing all day til now:)