GEMMA M0 - Multiple Buttons on 1 Analog Pin

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
JohnBottle
 
Posts: 23
Joined: Sat Apr 09, 2022 2:32 pm

GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by JohnBottle »

Hi all,

I've been trying to copy this circuit from Arduino, and using it with my GEMMA M0 on the A0 Pin.

The circuit is:
FH3XBN0IBKMKXP3.png
FH3XBN0IBKMKXP3.png (47.16 KiB) Viewed 220 times
And you measure the resistance as each button is pressed.

I "kind" of have it working - but the AnalogIn just gives random values between 0 and 3v - in waves, as seen in the CircuitPython guide here:

https://learn.adafruit.com/adafruit-gem ... nalog-in-2

With no contact on A2 - why is the Analog value not 0v? If the voltage was just 0v, until a button was pressed, it would be very easy to do this project.


The guide I followed is here:

https://www.instructables.com/How-to-Mu ... rduino-Tu/

Any advice would be great.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by dastels »

With the 100K as the bottom of the divider you may want larger values on the top sides to space out the values more. Maybe ones that are 10 times bigger? So instead of 4.95v, 4.9v, 4.85v, and 4.5v you would get 4.5v, 4.16v, 3.12, and 2.5v. The bigger spacing will make it easier to discern which switch is pressed and make resistor tolerances less of an issue.

With the SAMD21 analog input you probably want to use 3.3v rather than 5v.

Dave

User avatar
JohnBottle
 
Posts: 23
Joined: Sat Apr 09, 2022 2:32 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by JohnBottle »

Thanks for the reply Dave!

I should have said - I’m using the 3.3v out.

So should this be technically possible with the Gemma, and only 3.3v??

The analog output just seems to be a continuous wave from 0v to 3v - with no buttons being pressed - so at some point in the wave, the code “thinks” a button has been pressed.

The demo here shows it perfectly - the analog is never just “0v” - even with no connections or voltage to the pin.

https://learn.adafruit.com/adafruit-gem ... nalog-in-2

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by dastels »

Yes, the Gemma M0 should have no trouble doing this.

"Output"?

Anyway, can you run the top example on https://learn.adafruit.com/adafruit-gem ... nalog-in-2 and post the output with no buttons pressed?

Dave

User avatar
JohnBottle
 
Posts: 23
Joined: Sat Apr 09, 2022 2:32 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by JohnBottle »

Thanks again for the follow up.

I get exactly the results you see in the example/demo when no pin is touched. Like this:
20406514-7ED3-4F9F-81D6-A84478D258DB.png
20406514-7ED3-4F9F-81D6-A84478D258DB.png (141.46 KiB) Viewed 197 times
No pin being touched - gives results like this:
(You would imagine it would just be 0v?)

Code: Select all

(2.13199 ,)
(1.28498,)
(0.778171,)
(0.851939,)
(1.6638,)
(2.47666,)
(2.95236,)
(2.397 ,)
(1.57215,)
(0.879432,)
(0.44951,)
(0.596443,)
(1.18533,)
(1.87221,)
(2.56569,)
(2.72385,)
(1.97936,)
(1.3017,)
(0.693928,)
When 3.3 is applied to the pin - it does show (3.3) repeated. But as soon as you let go, it goes back to the random values between 0 and 3

User avatar
adafruit_support_bill
 
Posts: 88089
Joined: Sat Feb 07, 2009 10:11 am

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by adafruit_support_bill »

Looks like the input is floating. Check the connections on your 100K pull-down resistor.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by dastels »

Yes, it's acting like a touch input. And yes, check the resistor connections.

Dave

User avatar
JohnBottle
 
Posts: 23
Joined: Sat Apr 09, 2022 2:32 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by JohnBottle »

dastels wrote:Yes, it's acting like a touch input. And yes, check the resistor connections.

Dave
Do I need to tell the code that it is not a touch input somewhere? I've checked the resistor connections - and they look fine.

This is my circuit - i've decided to try it on the 5v out - as it let's me copy another circuit design i found here: http://www.ignorantofthings.com/2018/07 ... istor.html
Screenshot 2022-04-22 173127.png
Screenshot 2022-04-22 173127.png (189.97 KiB) Viewed 187 times

This is my code:

Code: Select all

import time
import board
from analogio import AnalogIn

analog_in = AnalogIn(board.A1)

# Helper to convert analog input to voltage
def get_voltage(pin):
    return (pin.value * 3.3) / 65536

while True:
    print((get_voltage(analog_in),))
    time.sleep(0.1)



And this is the serial and plotter results when NO buttons are pressed - i'm sure my soldering is fine.
Screenshot 2022-04-22 173316.png
Screenshot 2022-04-22 173316.png (27.32 KiB) Viewed 187 times

Code: Select all

(2.67883,)
(2.64016,)
(2.46453,)
(2.03108,)
(1.51223,)
(0.924902,)
(0.749267,)
(0.384302,)
(0.501929,)
(0.652588,)
(1.02642,)
(1.66611,)
(2.02222,)
(2.32192,)
If i disconnect EVERYTHING from the Gemma, the serial shows this (when nothing is attached to A1 pin), a roughly stable 1.4x voltage.

Code: Select all

(1.28664,)
(1.42522,)
(1.43972,)
(1.43247,)



Thanks so much for any help with this!

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by dastels »

You need to use 3.3v. The SAMD21 is a 3.3v MCU and can only measure between 0v and 3.3v. Putting more then 3.3v on an input can damage the MCU. Also, your code assumed a 3.3v max.

If you use AnalogIn you will be fine.

Another circuit to try is the one described in #5 on https://www.baldengineer.com/5-voltage- ... cuits.html.

The thing is that you've added wires that are acting like antenna and picking up EMF which gets read as a voltage. How long are the wires? What's the frequency of that rough sine wave you're seeing?

Dave

User avatar
JohnBottle
 
Posts: 23
Joined: Sat Apr 09, 2022 2:32 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by JohnBottle »

Guys... I have found the problem, but I am unsure what the reason is.

It comes down to a single wire - going from the start of the resistors - to the A1 pin on the Gemma - shown here:
Screenshot-2022-04-22-201445.jpg
Screenshot-2022-04-22-201445.jpg (303.03 KiB) Viewed 185 times
If i use a crocodile clip - everything works fine!!

If I use my soldered wire - the voltage goes up, and down, up, and down.

But the soldering looks fine!! I really do not know what problem is. On big projects - how can you possibly track down if a wire is not working correctly.

Thanks for your help through this issue anyway!

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: GEMMA M0 - Multiple Buttons on 1 Analog Pin

Post by dastels »

It happens. Maybe a weak point or break in the wire from being bent too often.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”