Controlling electronics with Python - a quick start with pyMCU board

pyMCU is an electronic board that can handle electronic components like various sensors (temperature, light, pressure, magnetic field, distance etc.) or for example small alphanumeric displays. All of that is done by code you can write in Python. The board is equipped with a PIC 16F1939 microcontroller offering 13 Digital IO Pins, 6 Analog IO Pins, 5 10-bit PWM Pins, and a 16 Pin Parallel LCD Interface. The board can be connected to nearly any computer via provided mini USB port. There are drivers for Linux, Windows and OSX. You can find more information on project website.

Compared to for example Raspberry Pi and other mini-computers this board isn't a computer and has no CPU or RAM like those mini computers. It's controlled from the computer to which it's connected. It can be a PC, laptop or also Raspberry and some other mini-computers. It provides similar features as Raspberry GPIO pins (or Arduino etc.). By default it uses 5V so it can be used as an addition to 3.3V Raspberry or to work with 5V Arduino components. A handy tool for you self-made electronics project.

pyMCU can be ordered from the shop for around 20 EUR or $25. Is cheaper than Raspberry as it's a simpler board - no CPU, RAM or other peripherals.

If you are new to electronic components here is a quick introduction (I'll write more about that in another article later). You will get the pyMCU with the USB cable from the shop, but to start practicing on some simple electronics circuits you will need some components from your local electronics store (or stores like Adafruit, raspberry/arduino-related shops etc.). Here is a quick list of most handy components:

breadboard - you can stick electronic components and wires in it to do a simple circuit

male-male jumper wires - the wires to connect pyMCU with the breadboard

LEDs for basic circuits

a set of resistors (to prevent LED burning out etc.)

you can also look at things like potentiometer (variable resistor) or photodiode, photoresistor that are light sensors

A "Hello World" for such electronic devices is to turn on and off a LED using some code. The LEDs have their voltage and current specified. Those parameters are important as wrong LED won't turn on or will burn quickly (stop glowing). As pyMCU uses 5V you must use a LED that has voltage lower than the 5V. You will have to also have an resistor to prevent it from burning. For a simple pick few hundred Ohms resistor will be good (or a potentiometer - where you can set resistance from a given range). Stronger resistors will make the light dimmer. Very strong resistors (a lot of kilo or mega Ohms) may prevent it from glowing.

A lot of sensors like photodiodes, photoresistors or some temperature sensors are resistors that change their resistance (like photoresistor will change its resistance depending wherever it's dark or bright). Those simple and cheap components can also be used in simple circuits for practice. Note that there are also more advanced sensors that may require more complicated circuits to work (like some sensors in the shape of Arduino boards).

If you will dive deeper into the electronics world you will need to solder some elements together. You will need a soldering iron, solder (tin) and rosin to do it... You can find some tutorials on Adafruit website and YouTube channel. For circuits shown in this tutorial the breadboard and jumper wires will do just fine.

To work with pyMCU you will need drivers for it's USB serial adapter. On Linux they are in the system out of the box. Windows should install them on its own while on OSX you have to install them. After that you will have to install the Python libraries:

pip install pyserial pymcu

KERNEL=="ttyUSB0", MODE="0666"

Blinking a LED is the first simple circuit you can do with pyMCU. It's available on the tutorials page too.

LED will have a longer and shorter pin. Connect the shorter to D1 socket on pyMCU (via jumper wire and breadboard), and the longer one via resistor to GND socket.

The resistor should have few hundred Ohms. You can try to calculate the minimal resistance from the equation (5 - X)/Y where X is the LED voltage and Y is the LED current.

import pymcu mb = pymcu . mcuModule () for x in range ( 1 , 25 ): mb . pinHigh ( 1 ) # Set D1 pin High (LED On) mb . pausems ( 500 ) # Sleep for half a second mb . pinLow ( 1 ) # Set D1 pin Low (LED Off) mb . pausems ( 500 ) # Sleep for half a second

You can connect a photoresistor or photodiode in the place of the resistor. You should notice that the LED will blink brighter or fainter depending how much light falls onto the sensor (for photodiode it won't blink if it will be connected the wrong way - photodiode is also one direction only component).

More fun with electronic components, sensors in next article.

RkBlog