Skip to content

Commit 460b9f3

Browse files
committed
docs: Analog IO guide update.
1 parent 1712c75 commit 460b9f3

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

docs/programming_guide/01_digital_io.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,6 @@ too, for example:
315315
>>> led = DigitalInOut(board.A1)
316316
>>> led.direction = Direction.OUTPUT
317317
>>> led.pull = Pull.UP
318+
319+
.. Author: Tony DiCola
320+
Copyright: 2017

docs/programming_guide/02_analog_io.rst

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ analog signals and voltages with a microprocessor.
6767
Typically development boards have one or more built-in analog to digital
6868
converters. Check your board's documentation for details on the number of ADCs
6969
and which pins are used as inputs for them (some boards like the Metro M0
70-
Express and Circuit Playground Express note their analog inputs with names like
71-
A0, A1, etc.).
70+
Express, Circuit Playground Express, Trinket M0, and Gemma M0 note their analog
71+
inputs with names like A0, A1, etc.).
7272

7373
Along with the number of analog to digital converters your board's documentation
7474
also might mention the resolution or 'bits' used by the ADC. The resolution of
@@ -206,6 +206,14 @@ voltage using the reference voltage:
206206
Twist the potentiometer knob and run the same line again to see how the voltage
207207
value changes!
208208

209+
You can also wrap the above equation into a Python function that's easy to call
210+
and convert ADC values into voltages:
211+
212+
>>> def adc_to_voltage(val):
213+
... return val / 65535 * 3.3
214+
>>> adc_to_voltage(adc.value)
215+
3.2998
216+
209217
Digital to Analog Converter (Outputs)
210218
--------------------------------------
211219

@@ -233,7 +241,7 @@ Connect the components to your board as follows:
233241

234242
- The short leg (cathode) of the LED connects to one end of the resistor.
235243
- The other end of the resistor connects to the ground or GND pin of the board.
236-
- The long leg (anode) of the LED connects to a the digital to analog converter output of your board. You might need to check your board's documentation to find this pin. On the Metro M0 Express and Circuit Playground Express looks for the A0 pin with a squiggly line next to it (the squiggle indicates this pin is a DAC output).
244+
- The long leg (anode) of the LED connects to a the digital to analog converter output of your board. You might need to check your board's documentation to find this pin. On the Metro M0 Express and Circuit Playground Express look for the A0 pin with a squiggly line next to it (the squiggle indicates this pin is a DAC output).
237245

238246
Now at the REPL import the :py:mod:`analogio` and :py:mod:`board` module to
239247
create an instance of the :py:class:`analogio.AnalogOut` class:
@@ -280,6 +288,8 @@ on the A0 output or LED anode and the negative probe on the board ground, then
280288
measure the DC voltage. As you set the value see how voltage read by the
281289
multimeter changes!
282290

291+
.. image:: images/02_analog_io_multimeter.jpg
292+
283293
Just like with an analog input the digital to analog converter converts its
284294
digital value (the number like 10000) to a voltage based on an internal analog
285295
reference voltage. For the Metro M0 Express and Circuit Playground Express this
@@ -305,6 +315,13 @@ on the LED but not very brightly. Try setting the DAC value to other values
305315
above 50000 to see how an increase in the voltage increases the brightness of
306316
the LED!
307317

318+
Remember you can create a Python function to simplify setting the output value
319+
for a desired output voltage:
320+
321+
>>> def dac_value(volts):
322+
... return int(volts / 3.3 * 65535)
323+
>>> led.value = dac_value(2.5)
324+
308325
Pulse-width Modulation (Outputs)
309326
--------------------------------
310327

@@ -408,6 +425,17 @@ signal turned on for 1/3 of the time and turned off for the remaining 2/3 of the
408425
time. By manipulating the duty cycle you have similar control as if you were
409426
adjusting the voltage output by the pin!
410427

428+
To further illustrate how PWM is different from true analog output, look at the
429+
image below which shows oscilloscope output of a PWM signal at different duty
430+
cycles (0%, 33%, 50%, 66%, and 100%). Notice how as the duty cycle increases
431+
the amount of time the signal is at a high logic level (3.3V) gets longer. At
432+
66% duty cycle the signal is high for twice as long as at 33% duty cycle
433+
(compare how long the tops of each wave are to check for yourself). At the
434+
extremes of 0% and 100% you can also see the signal never changes and is always
435+
at a high or low level!
436+
437+
.. image:: images/02_analog_io_pwm.png
438+
411439
Back to controlling the LED, you can change the duty cycle by modifying the
412440
:py:attr:`pulseio.PWMOut.duty_cycle` attribute. Try setting the duty cycle to a
413441
100% or fully on value with:
@@ -459,3 +487,6 @@ Try using a loop to go through all 0-100% duty cycle values and back:
459487

460488
You should see the LED fade from off to fully on and back down to off
461489
repeatedly. Press Ctrl-C to stop the loop and get back to the serial REPL.
490+
491+
.. Author: Tony DiCola
492+
Copyright: 2017
Loading
Loading

0 commit comments

Comments
 (0)