You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/programming_guide/02_analog_io.rst
+34-3Lines changed: 34 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -67,8 +67,8 @@ analog signals and voltages with a microprocessor.
67
67
Typically development boards have one or more built-in analog to digital
68
68
converters. Check your board's documentation for details on the number of ADCs
69
69
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.).
72
72
73
73
Along with the number of analog to digital converters your board's documentation
74
74
also might mention the resolution or 'bits' used by the ADC. The resolution of
@@ -206,6 +206,14 @@ voltage using the reference voltage:
206
206
Twist the potentiometer knob and run the same line again to see how the voltage
207
207
value changes!
208
208
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
+
>>> defadc_to_voltage(val):
213
+
... return val /65535*3.3
214
+
>>> adc_to_voltage(adc.value)
215
+
3.2998
216
+
209
217
Digital to Analog Converter (Outputs)
210
218
--------------------------------------
211
219
@@ -233,7 +241,7 @@ Connect the components to your board as follows:
233
241
234
242
- The short leg (cathode) of the LED connects to one end of the resistor.
235
243
- 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).
237
245
238
246
Now at the REPL import the :py:mod:`analogio` and :py:mod:`board` module to
239
247
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
280
288
measure the DC voltage. As you set the value see how voltage read by the
281
289
multimeter changes!
282
290
291
+
.. image:: images/02_analog_io_multimeter.jpg
292
+
283
293
Just like with an analog input the digital to analog converter converts its
284
294
digital value (the number like 10000) to a voltage based on an internal analog
285
295
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
305
315
above 50000 to see how an increase in the voltage increases the brightness of
306
316
the LED!
307
317
318
+
Remember you can create a Python function to simplify setting the output value
319
+
for a desired output voltage:
320
+
321
+
>>> defdac_value(volts):
322
+
... returnint(volts /3.3*65535)
323
+
>>> led.value = dac_value(2.5)
324
+
308
325
Pulse-width Modulation (Outputs)
309
326
--------------------------------
310
327
@@ -408,6 +425,17 @@ signal turned on for 1/3 of the time and turned off for the remaining 2/3 of the
408
425
time. By manipulating the duty cycle you have similar control as if you were
409
426
adjusting the voltage output by the pin!
410
427
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
+
411
439
Back to controlling the LED, you can change the duty cycle by modifying the
412
440
:py:attr:`pulseio.PWMOut.duty_cycle` attribute. Try setting the duty cycle to a
413
441
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:
459
487
460
488
You should see the LED fade from off to fully on and back down to off
461
489
repeatedly. Press Ctrl-C to stop the loop and get back to the serial REPL.
0 commit comments