Using analog-digital magnetic field sensor with pyMCU

Check out the new site at

A Hall effect sensor is a sort of a switch that turns on or off if it is close to a strong magnetic field (close to a magnet). It can work as a motion or rotation sensor reacting to a part that moves back and to the sensor.

There is a lot of sensor board with Hall sensor made for Arduino. I used one of available board from You can find it also on ebay and at other shops.

Hall sensor board connected to pyMCU
Hall sensor in detail

The board has four pins: GND (-), ACC (+5V), A0 and D0. In case of pyMCU we just connect GND and ACC to matching sockets. If we want a digital signal (1 or 0) the we use the D0 connected to D1 on pyMCU. Analog value is available via A0 connected to A1 on pyMCU board. Double female and double male jumper wires will be needed to connect them together.

I've shown how to read analog values in previous article. This sensor board gave somewhere above 200 when there was no strong magnetic field and around 0 when it was placed close to a magnet. When reading digital value we have 0 with no magnetic field and 1 when the field is strong enough to trigger the sensor. With pyMCU you can read the digital value like so:

import pymcu

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

while 1:
    if mb.digitalRead(1):
      print 'South'
      print '.'
By default the digital socket on pyMCU works as an output so to read a value we have to set it to be an "input". Then we can read it's value with digitalRead. As you can see the script will print "South" if the magnetic field triggers the sensor. It's due to the fact that Hall sensors react to the south pole of magnets.

There is a lot of such plain boards for Arduino. Aside of that platform most of them should also work with pyMCU. For Raspberry you will quite likely need an extension board providing 5V logic as such voltage is used by Arduino - and Arduino compatible sensor boards and shields. I'll cover more complex interfaces like I2C or SPI in following articles.


Check out the new site at
Comment article
Comment article RkBlog main page Search RSS Contact