KAG Tech
Posts: 29
Joined: Mon Jun 21, 2021 7:11 am

Enabling and disable a GPIO

Tue Jul 20, 2021 9:39 am

Below is a program I used to read varying voltages from a variable resistor and it worked perfectly.

from machine import ADC, Pin
from time import sleep

adcX = ADC(0)
oldX = 0
conversion_factor = 3.3/65536

while True:
valX = adcX.read_u16()
sleep(5)
if valX != oldX:
print("X:", valX * conversion_factor, "volts")
oldX = valX

Please assist on how I can enable another GPIO when the input voltage on ADC 0 is equal to or above 2.3 V and disable it when the voltage reads equal to or below 2.2 V

fdufnews
Posts: 344
Joined: Fri Oct 07, 2011 5:37 pm

Re: Enabling and disable a GPIO

Tue Jul 20, 2021 9:53 am

Use the Pin class. Have a look here

hippy
Posts: 10310
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Enabling and disable a GPIO

Tue Jul 20, 2021 11:04 am

From first principles, an ADC will return a 16-bit 'Nadc' value as follows -

Nadc = Vin * 65535 / Vref

You can then determine what the voltage in was from -

Vin = Nadc * Vref / 65535

So one solution is to convert the ADC reading to the voltage in -

Code: Select all

Vref = 3.3
while True:
  Nadc = adcX.read_u16()
  Vin = Nadc * Vref / 65535
  if Vin >= 2.3:
    ...
  elif Vin <= 2.2:
    ...
Or, you could pre-calculate what 'Nadc' would be for the two voltage input limits -

Code: Select all

Vref = 3.3
Nadc_at_2_point_3_volts = 2.3 * 65535 / Vref
Nadc_at_2_point_2_volts = 2.2 * 65535 / Vref
while True:
  Nadc = adcX.read_u16()
  if Nadc >= Nadc_at_2_point_3_volts:
    ...
  elif Nadc <= Nadc_at_2_point_2_volts:
    ...

hippy
Posts: 10310
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Enabling and disable a GPIO

Tue Jul 20, 2021 11:24 am

Just a quick note on terminology -

"Enabling and disable a GPIO" - It's not exactly clear what "enable" or "disable" may mean. You will know what that means to you but others will understand that differently.

"Turn on" and "turn off" might be better choices, but what is "on" and what is "off" ? In some cases "on" may mean setting a pin high, but in other cases it may mean setting it low.

The recommendation is to be explicit as to setting a pin high or low, or to use the generic terms "asserted" and "deasserted" when it doesn't matter if "asserted" is high or low, may vary depending on hardware configuration.

KAG Tech
Posts: 29
Joined: Mon Jun 21, 2021 7:11 am

Re: Enabling and disable a GPIO

Thu Jul 22, 2021 9:03 am

fdufnews wrote:
Tue Jul 20, 2021 9:53 am
Use the Pin class. Have a look here
Thank you for the reply, the documentation was of great help especially with commands we did not know of.
We managed to figure it(the program) out. It was surprisingly as simple as using >= or <= and it worked perfectly fine.

Below is how we programmed Pi Pico:

from machine import ADC, Pin
from time import sleep

POT = ADC(0)
LED = machine.Pin(7, machine.Pin.OUT)

while True:
volt = POT.read_u16()
if volt <= 43590:
LED.value(0)

elif volt >= 43591:
LED.value(1)

KAG Tech
Posts: 29
Joined: Mon Jun 21, 2021 7:11 am

Re: Enabling and disable a GPIO

Thu Jul 22, 2021 9:15 am

hippy wrote:
Tue Jul 20, 2021 11:04 am
From first principles, an ADC will return a 16-bit 'Nadc' value as follows -

Nadc = Vin * 65535 / Vref

You can then determine what the voltage in was from -

Vin = Nadc * Vref / 65535

So one solution is to convert the ADC reading to the voltage in -

Code: Select all

Vref = 3.3
while True:
  Nadc = adcX.read_u16()
  Vin = Nadc * Vref / 65535
  if Vin >= 2.3:
    ...
  elif Vin <= 2.2:
    ...
Or, you could pre-calculate what 'Nadc' would be for the two voltage input limits -

Code: Select all

Vref = 3.3
Nadc_at_2_point_3_volts = 2.3 * 65535 / Vref
Nadc_at_2_point_2_volts = 2.2 * 65535 / Vref
while True:
  Nadc = adcX.read_u16()
  if Nadc >= Nadc_at_2_point_3_volts:
    ...
  elif Nadc <= Nadc_at_2_point_2_volts:
    ...
Thank you for the reply. We managed to figure it out and it worked perfectly fine but we will definitely try your program too, it will help improve our programming skills since we are beginners.

Below is how we programmed the Pi Pico

from machine import ADC, Pin
from time import sleep

POT = ADC(0)
LED = machine.Pin(7, machine.Pin.OUT)

while True:
volt = POT.read_u16()
if volt <= 43590:
LED.value(0)

elif volt >= 43591:
LED.value(1)

KAG Tech
Posts: 29
Joined: Mon Jun 21, 2021 7:11 am

Re: Enabling and disable a GPIO

Thu Jul 22, 2021 9:17 am

hippy wrote:
Tue Jul 20, 2021 11:24 am
Just a quick note on terminology -

"Enabling and disable a GPIO" - It's not exactly clear what "enable" or "disable" may mean. You will know what that means to you but others will understand that differently.

"Turn on" and "turn off" might be better choices, but what is "on" and what is "off" ? In some cases "on" may mean setting a pin high, but in other cases it may mean setting it low.

The recommendation is to be explicit as to setting a pin high or low, or to use the generic terms "asserted" and "deasserted" when it doesn't matter if "asserted" is high or low, may vary depending on hardware configuration.

Noted. Thank you for the reply.

fdufnews
Posts: 344
Joined: Fri Oct 07, 2011 5:37 pm

Re: Enabling and disable a GPIO

Thu Jul 22, 2021 10:13 am

Please use the Code tag on the tool bar to put your code online otherwise it will not be correctly formated.
All the indentations are lost.

Return to “MicroPython”