RkBlog

Hardware, programming and astronomy tutorials and reviews.

Reading analog sensor values using pyMCU

How to read values from light, temperature or magnetic field sensors using pyMCU and some Python code

One of pyMCU handy features is the ability to read analog values - voltage in the current altered by a sensor or potentiometer - electronics that alters its resistance depending on conditions. There are photodiodes, phototransistors, photoresistors (light sensors, thermocouples and other temperature or magnetic field sensors (Hall sensor) that work in such analog way. In this article I'll show you how to read values from those sensors using pyMCU and some Python code.

Reading the value

We can read the analog value using pymcu analogRead(PIN_NUMBER) method. PyMCU has six analog outputs (A1-A6) so we can work with up to six sensors. The method will return a value from 0-1023 range, where 0 is no voltage (like circuit not connected), while 1023 is nominal voltage (no loss on the circuit). For a light sensor 0 would be dark, and 1023 bright.

For a basic circuit reading analog sensor value we will need the sensor and a resistor to match the sensor resistance and provide good match of sensor change in resistance to noticeable change or read values. For average light sensor you can try with 10 kΩ resistor at start. The circuit looks like so:

Photoresistor circuit

We connect the sensor from one side to VCC(+) and from the other one through a resistor to GND(-). Analog output measuring the value is between the sensor and the resistor.

Phototransitor circuit
Photoresistor
Temperature sensor
A basic Python script reading the value constantly looks like so:
import pymcu

mb = pymcu.mcuModule()

while 1:
    print mb.analogRead(1)

10 kΩ worked quite nicely with my light sensors giving few hundred units change between dark and bright conditions. The temperature sensor required much lower resistance. The phototransistor L53P3BT I used is sensitive to infrared light (940 nm). Those type of transistors work when connected in reverse. Photoresistors aren't polarized and connecting them any way will give the same results.

Potentiometer is not a sensor but a resistor with settable resistance. It's used in some electronic devices to set things like sound volume, fan speed or brightness. Using potentiometer is described on pyMCU website and it's bit easier than reading a sensor value, as the potentiometer provides a third pin to which we connect the analog wire. No extra resistor is needed.

You can connect the potentiometer both ways. If connected in reverse the read values will also be reversed (like 1023 for turned of potentiometer).

Potentiometer circuit

Calibration and getting real units

When using a temperature sensor you are not reading temperature in direct. To map read values to a temperature value you have to calibrate such sensor. Measure values for known temperatures and then calculate an equation (usually linear or logarithmic) describing a relation between analog value and the real temperature value. The same thing for other sensors.

RkBlog

Raspberry Pi, mini computers, Android and microcontrollers, 9 June 2013,

Comment article