Voltage on Analog Pins with nothing plugged in?

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
eat_sleep_code
 
Posts: 64
Joined: Sat Dec 30, 2017 1:30 am

Voltage on Analog Pins with nothing plugged in?

Post by eat_sleep_code »

I have the code similar to the following:

Code: Select all

pin1 = AnalogIn(board.D1)
pin1Threshold = 1.6

def getVoltage(pin):
    voltage = (pin.value * 3.3) / 65536
    return voltage

def gitPin(pin)
    if pin == 1:
        return pin1Threshold, getVoltage(pin1)

pinNumber = 1
threshold, voltage = getPin(pinNumber)
print('Threshold: ', threshold)
print('Voltage: ', voltage)
In my actual code, I have this for pins 1 - 4, but edited here for brevity.

The voltage of the pins with nothing plugged in varies between 0.8 to 2.2volts with nothing plugged in. This is with just a Trinket M0 plugged into the desktop using the micro USB.

Why is this? I would think the voltage should be 0 if there is nothing plugged in?

I am running the latest (as of yesterday) production release versions of both CircuitPython and the boot loader (was happening on the original version too, upgraded to see if it was a bug).

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

Re: Voltage on Analog Pins with nothing plugged in?

Post by adafruit_support_bill »

With nothing connected, the inputs will 'float' and the values will be unpredictable. If you want to see a 0, you need to tie the input to ground either directly or via a pulldown resistor.

User avatar
eat_sleep_code
 
Posts: 64
Joined: Sat Dec 30, 2017 1:30 am

Re: Voltage on Analog Pins with nothing plugged in?

Post by eat_sleep_code »

Thanks!

User avatar
maesoph
 
Posts: 51
Joined: Thu Mar 03, 2016 9:04 pm

Re: Voltage on Analog Pins with nothing plugged in?

Post by maesoph »

Fair warning: Mechanical engineer that thinkers with electronics.......

Working with a Trinket M0 - does it have a built in pull up / pull down resistor???

I've tried both schemes and the voltage just seems to float about in the middle of the range.. Assuming the answer is likely "NO"..... What is the minimum resistance that should be used for pull up / pull down resistors?? I have a very limited selection of resistors - don't think I have anything in the neighborhood of 10k ohms...

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

Re: Voltage on Analog Pins with nothing plugged in?

Post by adafruit_support_bill »

These processors do have built-in pullups, but you need to explicitly enable them with a PinMode statement: https://www.arduino.cc/reference/en/lan ... o/pinmode/

The exact value is usually not critical. 10K is commonly used. 1K would be considered a fairly 'strong' pullup. 100K would be on the 'weak' side. The internal pullups are typically in the 20K Ohm range.

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

Return to “Trinket ATTiny, Trinket M0”