The board
As an embedded enthusiast, I finally decided to get my hands on a pyboard and start some MicroPython programming.
This is a picture of the board I got from DigiKey Electronics, including pin sockets already soldered on it. It is actually a very compact board, a bit bigger than a 2€ coin, and features an ARM® Cortex®-M4 processor with 1MB Flash ROM and 192kB RAM. Make sure you print out its pinout and keep it handy all the time.
Once connected to my MacBook Pro using a micro USB cable, it appears as a USB drive named PYBFLASH. The content of the README.txt file provides you instructions about how to get started or get a serial prompt, all shown in the screen shot below.
Updating the firmware
As you can see from the screenshot, I updated the firmware to the latest available, which at the moment of the current writing is version 1.16. Since it took me a while to spot the link with the instructions for updating the firmware, I'll provide it here for your convenience: you won't find it if you keep looking for it in the documentation! I also recommend that you fork and clone the MicroPython repository on GitHub, since you can update the firmware by using the Python DFU programmer located inside the tools folder of the repository.
For putting the board in update mode, you don't need to apply any external wiring: from the serial prompt, type the following commands and you are ready to go:
import machine
machine.bootloader()
I downloaded the following DFU file from this link:
pybv11-20210618-v1.16.dfu
and saved it under the tools folder of my local MicroPython repository.
Next I installed the required dependencies for using the Python DFU programmer pydfu.py, i.e. PyUSB and libusb, via the Python pip installer and HomeBrew, respectively.
Finally, I triggered the update from the shell prompt and was done:
$ python pydfu.py -u <firmware.dfu>
Generating your first sine wave
The main advantage of MicroPython is that it provides high level, direct access to the hardware it runs on, in a programmer friendly and efficient language as Python.
Test code can be either input on the interactive MicroPython shell or added to the main.py script, as shown below, where I have edited the file with IDLE.
The main.py script is then automatically started as soon as the board is powered, e.g. via USB from a PC or laptop.
I have slightly modified the example of the DAC that you can find in the online documentation, by halving the dynamic of the generated sine wave, in order to avoid any output saturation of the converter, and by doubling the sample size, in order to produce a "finer" output signal.
# main.py -- put your code here!
import math
from array import array
from pyb import DAC
# create a buffer containing a sine-wave, using half-word samples
buf = array('H', 2048 + int(1023 * math.sin(2 * math.pi * i / 256)) for i in range(256))
# output the sine-wave at 440Hz
dac = DAC(1, bits=12)
dac.write_timed(buf, 440 * len(buf), mode=DAC.CIRCULAR)
The sine wave is output on pin socket X5 and connected to channel 1 of
The captured waveform is shown below, where a nice sine wave at 440 Hz can be seen. Easy-peasy!
Finally, the following reference has been consulted for creating this post:
Comments