Using Arduino sensor boards with pyMCU
Using buzzer, ultrasonic distance sensor and an alcohol sensor 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.
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:
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
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.
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 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:
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.