Using Arduino sensor boards with pyMCU

There is a lot of boards that can be used with Arduino. There always will be something new to test and use. In this article I'll present three boards working with pyMCU - buzzer, ultrasonic distance sensor and an alcohol sensor.

Passive buzzer

Passive buzzer connected to pyMCU

A buzzer is a type of a simple speaker capable of beeping with different tones. It has only three pins: GND (-), VCC (+) and signal pin (marked as "S") which should be connected to digital pin on pyMCU (D1 for example). The buzzing is generated by frequency cast on the signal pin. In pyMCU it can be done in three ways:

for freq in range(0, 32):
    mb.freqOut(1, 500, freq*1000)

for i in range(1, 500):

mb.dtmfOut(1, 1)
mb.dtmfOut(1, 2)
mb.dtmfOut(1, 3)
mb.dtmfOut(1, 4)
mb.dtmfOut(1, 5)
mb.dtmfOut(1, 6)
mb.dtmfOut(1, 7)
mb.dtmfOut(1, 8)
mb.dtmfOut(1, 9)
mb.dtmfOut(1, 0)

First solution is to use freqOut method to generate a given frequency on given digital pin. Aside of pin number this method accepts duration (ms) and frequency (Hz, from 0 to 32767 Hz). The buzzer won't handle all those frequencies so expect its tone to change randomly as the frequency increases.

Second solution is to switch the pin between 0 and 1 (pinHigh, pinLow) rapidly. Using this solution I was able to make it beep loudly.

Third solution is to use dtmfOut method, that generates frequencies for DTMF signalization (tones on numeric keypad of older phones). We have keys from 0-9 to choose from.

MQ-3 alcohol vapor sensor

Alcohol vapor sensor

MQ-3 alcohol vapor sensor is one of gas sensors available for Arduino and other systems. It has four pins: VCC (+), GND (-), D0 (digital) and A0 (analog). You can use it just like the Hall sensor I described before. The model I have has reversed response - it returns 0 when it detects alcohol vapors.

import pymcu

mb = pymcu.mcuModule()
mb.digitalState(1, 'input')

while 1:
    if not mb.digitalRead(1):
        print 'hit'
        print '.'

There is also a small potentiometer to control sensor sensitivity level. When there are no vapors it must return 1 and when there are some it must fall to 0 (or vice versa depending on board model and behavior).

When vapor will be detected and you move it away from the source there will be some time lag before the sensor will respond. The vapors must leave its chamber.

Ultrasonic distance sensor HC-SR04

Ultrasonic distance sensor HC-SR04
Ultrasonic distance sensor connected to pyMCU

Ultrasonic distance sensor is bit more complicated to handle. It has four pins: VCC (+), Trig (connect it to digital pin), Echo (to digital pin), GND (-). I used D1 for "Trig" and D2 for "Echo".

Sending a 10 microsecond (some say exactly 10, some 10 or more) signal to the "Trig" pin causes the sensor to send eight ultrasonic pulses. If they will bounce back from something then they will be caught by sensor and we will get a signal on the "Echo" pin. Duration of the signal on the "Echo" pin allows calculating the distance to the obstacle.

Duration of the triggering signal may affect how the sensor performs. It's good to test few options and pick the best one for your sensor model and usage.

pyMCU can create a 10 microsecond signal. The signal duration can be somewhat measured too:

from datetime import datetime
import pymcu

mb = pymcu.mcuModule()
mb.digitalState(2, 'input')

start =

response_start = None
response_end = None

for i in range(0, 500):
    ret = mb.digitalRead(2)
    if ret == 1:
        end =
        response_end = end
        if not response_start:
            response_start = end

if response_end and response_start:
    echo_delta = response_end - response_start
    delta = echo_delta.total_seconds()
    print (delta * 340) / 2, ' m'
    print 'no echo captured'

The script uses pauseus to create a 10 ms signal pulse. Next it stats to listen to "Echo" pin response (in the range loop). It catches time of beginning and end of the high state of the echo pin. Next we calculate the time difference (seconds) which in the end give distance to the obstacle.

In my case this script allowed sensing distance from around half meter. On shorter distances it probably received the echo before the script started listening.

Comment article
Comment article RkBlog main page Search RSS Contact