I am using a thumb joystick for Arduino UNO, trying to work with it.
There are x
and y
values that I get with analog input A0
and A1
, and one z
value for the click that I should have gotten from the digital input D7
, but unfortunately I wired it to another anolog input (A3
).
This is the wiring from the specification
So, now that I fixed my mistake, I get the x
from A1
and y
from A2
, but z
is always zero. Is there a chance that the joystick part is physically damaged? Or worse, my Arduino UNO? How can I check?
int sensorPin = 5;
int value = 0;
void setup() {
pinMode(7, OUTPUT);
Serial.begin(9600);
}
void loop() {
value = analogRead(0);
Serial.print("X:");
Serial.print(value, DEC);
value = analogRead(1);
Serial.print(" | Y:");
Serial.print(value, DEC);
value = digitalRead(7);
Serial.print(" | Z: ");
Serial.println(value, DEC);
delay(100);
}
Chinese specific specification is here. I wish it was also available in English! :-/
1 Answer 1
Did I damage analog joystick clicker by using analog instead of digital input?
No. Neither mode will draw damaging amounts of current through the device.
But it does look like you've set pin 7 to be an output, and are trying to read digital values from that pin. Is that what you intended? :)
Make it an input instead and generally give your code a close examination line by line.
Further info: Setting a pin to be an input will configure your microcontroller to use "High-Z" mode on that particular pin. This means that the pin becomes a high impedance input ("Z" is the standard letter for impedance). This means that almost no current is able to be either drawn from, or sunk into, that pin.
While in High-Z state (also known as "tri-stated", or simply as "input") this pin is able to do either digital input readings (logic levels... 0's and 1's) or analog readings (an 8-bit analog input is able to give you a value between 0x00 and 0xff), which you can scale mathematically to a voltage between 0V and VCC.
-
\$\begingroup\$ The code is actually from the specification, and I think it reads from digital pin by setting
pinMode(7, OUTPUT)
and thendigitalRead(7)
. Isn't it the case? \$\endgroup\$Ho1– Ho12016年11月24日 21:56:42 +00:00Commented Nov 24, 2016 at 21:56 -
\$\begingroup\$ Please do not be offended, but your "Question" is not about that. It is about whether or not you have damaged your device. :) It is not meaningful to read values from an output pin, since your own code sets the output value itself. \$\endgroup\$user98663– user986632016年11月24日 21:59:23 +00:00Commented Nov 24, 2016 at 21:59
-
\$\begingroup\$ @Ho1 -- If you need a source to feel better, read a button state by using pinMode(X, INPUT): arduino.cc/en/Tutorial/Button \$\endgroup\$Wesley Lee– Wesley Lee2016年11月24日 22:01:27 +00:00Commented Nov 24, 2016 at 22:01
-
\$\begingroup\$ @Ho1 -- Actually it can be more harmful to set a pin as Output and then short it to 5V or GND with a button than switching analogRead for digitalRead. \$\endgroup\$Wesley Lee– Wesley Lee2016年11月24日 22:03:29 +00:00Commented Nov 24, 2016 at 22:03
-
1\$\begingroup\$ I suspect that your changing "OUTPUT" to "INPUT" on pin 7 solved it for you but for future reference, you can either just upload a screenshot of your diagram or use the stack exchange circuit diagram editor, which you can activate by clicking the icon in the "question editor" toolbar. Personally I just upload a picture most of the time. \$\endgroup\$user98663– user986632016年11月24日 22:21:08 +00:00Commented Nov 24, 2016 at 22:21
Explore related questions
See similar questions with these tags.
digitalRead(7)
. Isn't it the case? Btw, Google translate did a good job in translating from Chinese to English for me; Unfortunately after the fact. :-/ \$\endgroup\$