Raspberry Pi Pico Tips and Tricks

Tuesday 3 March 2015

Raspberry Pi GPIO Sensors Part 1: Measurement

The following post is a section of the book 'Raspberry Pi: Measure, Record, Explore'.  The entire book can be downloaded in pdf format for free from Leanpub or you can read it online here.
Since this post is a snapshot in time. I recommend that you download a copy of the book which is updated frequently to improve and expand the content.
This is the first of three posts working through a project looking at Measuring Recording and Exploring simple GPIO sensor inputs with the Raspberry Pi.

Basic GPIO Input Sensors

This project will use a Hall effect sensor to measure a change in a magnetic field and use that change to trigger the recording of a reading to our database. This type of sensor outputs a logical ‘high’ (1) when triggered and as such we can use the same set-up on our Raspberry Pi for a range of sensors.
The practical application that we will examine is to determine when a cat flap has been opened and using the times that this occurs we will build up a visualization of the pattern of activity that the cat exhibits as it goes in and out the cat flap.
The ‘Cattterplot’ Graph

The Hall Effect

The Hall effect is the production of a voltage difference (the Hall voltage) across an electrical conductor, transverse to an electric current in the conductor and a magnetic field perpendicular to the current. It was discovered by Edwin Hall in 1879.
A Hall effect sensor is a transducer that varies its output voltage in response to a magnetic field. Hall effect sensors are used for a range of measurement functions including proximity switching, positioning, speed detection, and current sensing applications.
For this project we will use a Hall effect sensor combined with circuitry that allows the device to act as a switch. In particular we will use the pre-built sensor ‘KY003’ which incorporates a series 3144 integrated circuit that contains the hall effect sensor proper (and some supporting circuitry). When the sensor is exposed to a high density magnetic field the circuit will produce a logic ‘high’ output. The ‘KY003’ sensor is widely available at an extremely low cost (approximately $2). Just give the part number a Googling for possible sources.


Hardware required

  • KY003 Hall effect sensor
  • 3 x Jumper cables
  • A magnet (small Rare Earth type would be ideal)
The KY003 Hall Effect Sensor
The KY003 Hall effect sensor has three connections that we will need to connect to a 5V supply, a ‘Ground’ and a GPIO pin to read the signal from the sensor.

Keyes KY003 Hall Effect Sensor
It also incorporates a surface mounted LED that will illuminate when the sensor detects the presence of a magnetic field and is triggered.
Triggering the Hall Effect Sensor


The KY003 sensor should be connected with ground pin to a ground connector, the 5V pin to a 5V connector and the signal pin to a GPIO pin on the on the Raspberry Pi’s connector block. In the connection diagram below the ground is connected to pin 6, the 5V is connected to pin 4 and the signal is connected to pin 3 (GPIO 2)
The following diagram is a simplified view of the connection.
Hall Effect Sensor Connection
Connecting the sensor practically can be achieved in a number of ways. You could use a Pi Cobbler break out connector mounted on a bread board connected to the GPIO pins. But because the connection is relatively simple we could build a minimal configuration that will plug directly onto the appropriate GPIO pins using header connectors and jumper wire. The image below shows how simple this can be.
Physical Connection of Hall Effect Sensor


Once correctly connected, the sensor is ready to go. It can be simply tested by bringing a magnet into close proximity with the sensor and the LED should illuminate. When this occurs, we can make the assumption (at this stage) that the sensor is working and has placed ‘high’ or ‘1’ on the signal pin of the sensor and therefore is presenting a ‘high’ or 1’ to GPIO 2 on the Pi
Part two of this project recording GPIO sensor events can be found here.

The post above (and heaps of other stuff) is in the book 'Raspberry Pi: Measure, Record, Explore' that can be downloaded for free (or donate if you really want to :-)).

No comments:

Post a Comment