Pymate-Analog: Difference between revisions

From pymate.io wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 10: Line 10:
== About Analog Input ==
== About Analog Input ==


Any of the output pin can be switched from digital input to Analog Input.
Any of the Input pin can be switched from digital input to Analog Input.


Analog Input can read the value of the DC voltage applied to the analog Input pin.
Analog Input can read the value of the DC voltage applied to the analog Input pin.
Line 33: Line 33:
[[File:pymate-adc.png|640px]]
[[File:pymate-adc.png|640px]]


=== Coding : raw value ===
== Coding ==
=== Getting raw value ===


The following example query the state if imput 3 and displays the results on the REPL session.  
The following example query the state if imput 3 and displays the results on the REPL session.  
Line 50: Line 51:
Which displays values between 0 and 65535 in the REPL.
Which displays values between 0 and 65535 in the REPL.


=== Coding : voltage ===
=== Getting  voltage ===


This second example return the ADC value in Volts.
This second example return the ADC value in Volts.
Line 68: Line 69:
</syntaxhighlight>
</syntaxhighlight>


== Example ==
Make the '''IN3''' acting as ADC INPUT in range 0..10 Volts (raw value in 0..65535).
Light the outputs 1 to 8 depending on the current voltage on IN3 (from 1..8 LEDs for 1V to +8V).
Also display the voltage value on REPL every half second.
{{dbox-gray|{{underline|'''Note:'''}}<br />
This example use the {{fname|TimeoutTimer}} class to avoids mainloop blocking while waiting an half-second.
Usage of {{fname|TimeoutTimer}} will be detailed in the [[Main_Page#Common_use-cases|common use-case]] tutorials.
}}
<syntaxhighlight lang="python" line>
from pymate.pymateio import PymateIO
from pymate import TimeoutTimer
from machine import Pin, ADC
import time
i3 = PymateIO( "IN3", ADC )
outputs = []
for idx in range(1,9): # iterate from 1..8
    outputs.append( PymateIO( "OUT%i" % idx, Pin.OUT ) )
timeout = TimeoutTimer()
timeout.set( 0.5 ) # Set 0.5 seconds (and start timer)
while True:
    value = i3.read_u16()
    # enumerate returns a tuple (idx, object).
    # idx is in the range from 0 to n-1 objects.
    for idx, out in enumerate( outputs ):
        v = i3.read_volts()
        out.write( v > idx+1 )
    # Display ADC value every half second
    if timeout.expired:
        print( "ADC : %.2f volts" % v )
        timeout.set( 0.5 ) # Restart timer
</syntaxhighlight>


{{PYMATE-TRAILER}}
{{PYMATE-TRAILER}}

Latest revision as of 22:06, 23 June 2026



Important note

Charts and graphics are made to be easy to follow.

The Pymate boards are drawed without connectors to avoids parallax shift. The real board are fitted with the connectors.

About Analog Input

Any of the Input pin can be switched from digital input to Analog Input.

Analog Input can read the value of the DC voltage applied to the analog Input pin.

The absolute maximum voltage is 10 Volts DC.

As the voltage is read through and Analog-to-Digital converter, the resulting value will be an unsigned integer.

As MicroPython defines the ADC.read_u16() method returning an unsigned 16 bits value (0 .. 65535).

About Upscaling:

Getting an uniform result encoded over 16bits makes the user script independent of the effective hardware resolution of the analog converter.

This means that read_u16() may returns an upscaled value when ADC converter offers lower resolution (below 16bits).

Keep in mind that code cannot create better resolution that hardware does!

Pymate ADCs have 12 bits resolution meaning that maximum accuracy is 2.44 mV (10 Volts / 4095).

Wiring

Pymate-adc.png

Coding

Getting raw value

The following example query the state if imput 3 and displays the results on the REPL session.

from pymate.pymateio import PymateIO
from machine import ADC
import time

a4 = PymateIO( "IN4", ADC )

while True:
    print( "Adc 4 RAW = %s" % a4.read_u16() )
    time.sleep( 0.2 )

Which displays values between 0 and 65535 in the REPL.

Getting voltage

This second example return the ADC value in Volts.

Note the "%.2f" formatting that limits the displayed value to 2 decimal digits (making the reading more comfortable).

from pymate.pymateio import PymateIO
from machine import ADC
import time

a4 = PymateIO( "IN4", ADC )

while True:
    v = a4.read_volts()
    print( "Adc 4 : %.2f volts" % v )
    time.sleep( 0.2 )

Example

Make the IN3 acting as ADC INPUT in range 0..10 Volts (raw value in 0..65535).

Light the outputs 1 to 8 depending on the current voltage on IN3 (from 1..8 LEDs for 1V to +8V).

Also display the voltage value on REPL every half second.

Note:

This example use the TimeoutTimer class to avoids mainloop blocking while waiting an half-second.

Usage of TimeoutTimer will be detailed in the common use-case tutorials.

from pymate.pymateio import PymateIO
from pymate import TimeoutTimer
from machine import Pin, ADC
import time

i3 = PymateIO( "IN3", ADC )

outputs = []
for idx in range(1,9): # iterate from 1..8
    outputs.append( PymateIO( "OUT%i" % idx, Pin.OUT ) )

timeout = TimeoutTimer()
timeout.set( 0.5 ) # Set 0.5 seconds (and start timer)
while True:
    value = i3.read_u16() 

    # enumerate returns a tuple (idx, object).
    # idx is in the range from 0 to n-1 objects.
    for idx, out in enumerate( outputs ):
        v = i3.read_volts()
        out.write( v > idx+1 )

    # Display ADC value every half second
    if timeout.expired:
        print( "ADC : %.2f volts" % v )
        timeout.set( 0.5 ) # Restart timer

PymateIO all right reserved © 2026 - Written by MCHobby for PymateIO