Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:58 28 Oct 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PICO 2W - MMBASIC - Trying to read analog joystick on ADC0 and ADC1

Author Message
Askjerry
Newbie

Joined: 21/09/2025
Location: United States
Posts: 11
Posted: 06:05pm 27 Sep 2025
Copy link to clipboard 
Print this post

I have been tinkering with this for a couple hours now... using GEMINI AI which is usually pretty good... but getting one failure after another.

In a perfect world, I would have two variables Jx and Jy, which would connect to ADC0 and ADC1, I'd read them maybe 10 times a second or so, and use the variables to do stuff.

The last attempt was...

' Define variables
DIM JX AS FLOAT
DIM JY AS FLOAT

' 1. Configure the pins as their ADC Channel
' Use the special syntax SETPIN <pin_number>, ADC<channel_number>
' GP26 is ADC Channel 0.
SETPIN 26, ADC0
' GP27 is ADC Channel 1.
SETPIN 27, ADC1

' --- Main Loop ---
DO
   ' 2. Read the analog voltage using the PIN function (Single Argument)
   ' Once configured as an ADC channel, PIN(n) should return the voltage.
   JX = PIN(26)
   JY = PIN(27)

   ' Display the values (JX and JY should be voltages from 0.0 to 3.3V)
   PRINT "JX (GP26/ADC0): "; JX; "V", "JY (GP27/ADC1): "; JY; "V"

   ' Wait for 0.1 seconds
   PAUSE 100
LOOP


Of course it's failing... anyone have any code that works?

Jerry
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6338
Posted: 08:32pm 27 Sep 2025
Copy link to clipboard 
Print this post

SETPIN GP26, AIN
SETPIN GP27, AIN


You can refer to pins by their GPxx number or physical pin number. With the proliferation of boards, it is safest to refer by GPxx
On a 'normal' pico board, pin 31 = GP26

ADC0 and ADC1 mean nothing to MMBasic. They are assumed to be a variable.
If you always use OPTION EXPLICIT, that will be evident.

Next time you are talking to Gemini, tell him/it to read the manual!

Jim
Edited 2025-09-28 07:05 by TassyJim
VK7JH
MMedit
 
Askjerry
Newbie

Joined: 21/09/2025
Location: United States
Posts: 11
Posted: 10:37pm 27 Sep 2025
Copy link to clipboard 
Print this post

I could have sworn that I tried doing a SETPIN GP26, AIN and that I got an error... But it's working well...

SetPin GP26, AIN
SetPin GP27, AIN
Do
Print Pin(GP26), Pin(GP27)
Loop


On the X-Axis I get 0.95 to 3.07 with 1.67 being the center, and on the Y-Axis I get 0.26 to 3.16 with 1.84 being the center.

Interesting... in Thonny I was getting a range that was much larger... about 12 bits. Probably a difference in how BASIC is reading the data.

This revision works...
' JOYSTICK EXPERIMENT - ADC0 and ADC1
' Define Pins as inputs
SetPin GP26, AIN
SetPin GP27, AIN
' Start working with the TFT Display
CLS
Do
text 220,10," JOYSTICK READINGS: "+ FORMAT$(Pin(GP26),"%.2f") + " , " + FORMAT$(Pin(GP27),"%.2f"),CM,2,,RGB(white),RGB(blue)
Pause 0.0100
loop

end

Edited 2025-09-28 09:04 by Askjerry
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8232
Posted: 07:05am 28 Sep 2025
Copy link to clipboard 
Print this post

Using PRINT PIN(GP26) will display the voltage relative to AVREF that's on the pin. So, if you use a LM4040 3V0 on AVREF and OPTION VCC 3 then GP26 will read from 0.0 to 3.0, with the mid point at 1.5. These are all floating point numbers, of course.

By default AVREF is tied to the 3V3 supply on the Pico so the mid point will be at about half the VCC rail, normally around 1.65. The accuracy of the centre point depends on the pots on the joystick. Few are accurate and the more expensive joysticks include trimmers. You can also compensate in software, of course. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Askjerry
Newbie

Joined: 21/09/2025
Location: United States
Posts: 11
Posted: 04:26pm 28 Sep 2025
Copy link to clipboard 
Print this post

This is the joystick that is part of the breakout board.

BREAKOUT BOARD
Edited 2025-09-29 02:27 by Askjerry
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8232
Posted: 05:08pm 28 Sep 2025
Copy link to clipboard 
Print this post

I refuse to guess then. :)
That's how MMBasic reads ADC inputs anyway. You get a voltage and it's up to you what you do with it. I usually scale it so that I get 0-100% of travel by multiplying the voltage by (100/3.3). Some fiddling with the endpoint voltages that you read centres it up.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10531
Posted: 05:42pm 28 Sep 2025
Copy link to clipboard 
Print this post

You can use SETPIN n,ARAW to get numbers in the range 0-4095 (manual page 153)
Edited 2025-09-29 03:43 by matherp
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025