Reading analog signal
-
Hello everyone,
I am trying to read the analog signal from a joystick using the Spresense main board connected to the extension board.
I had the joystick's VCC, GND, and VRx connected to extension board's VCC, GND, and A0. I am following the code bellow, which is from this link: https://github.com/sonydevworld/spresense-sketches/blob/master/analog_read/analog_read.inostatic const uint8_t pin_l = PIN_A0; static const uint8_t pin_h = PIN_A4; void setup() { // put your setup code here, to run once: pinMode(pin_l, INPUT); pinMode(pin_h, INPUT); pinMode(PIN_PWM_0, OUTPUT); analogWrite(PIN_PWM_0, 200); } void loop() { // put your main code here, to run repeatedly: delay(1000); int ret_l = analogRead(pin_l); int ret_h = analogRead(pin_h); printf("low = %d, high = %d\n", ret_l, ret_h); }
But after I upload the program and ran the Serial Monitor, it said:
"ERROR: Invalid pin number [128]
spresense dose not support using analog pin as digital."
"ERROR: Invalid pin number [132]
spresense dose not support using analog pin as digital."Can you give me any advice on this problem?
Thank you for your help.Best regards,
Tuan Nguyen -
You can remove the use of
pinMode
on analog pins.The code will look like this:
static const uint8_t pin_l = PIN_A0; static const uint8_t pin_h = PIN_A4; void setup() { // put your setup code here, to run once: pinMode(PIN_PWM_0, OUTPUT); analogWrite(PIN_PWM_0, 200); } void loop() { // put your main code here, to run repeatedly: delay(1000); int ret_l = analogRead(pin_l); int ret_h = analogRead(pin_h); printf("low = %d, high = %d\n", ret_l, ret_h); }
Best Regards,
Kamil Tomaszewski -
Hi @KamilTomaszewski ,
I have done what you said and it worked.
Thank you for your help.Best Regards,
Tuan Nguyen