Equipping a Raspberry Pi With Sensors

22.12.16    Raspberry Pi    electronics

I have used my Raspberry Pi before for GPIO projects. I have toyed around and created an abomination of a robot controlled via SSH or a web interface. But this was long ago.

Recently I decided to blow the dust off my old Raspberry Pi 1B and buy a package of sensors and emitters: Sunfounder's Sensor Kit v. 1.0. It is a nice package with many different kinds of sensors and emitters. All of these are easily connected to a Pi or on the provided breadboard with the supplied varieties of jumper cable.

Sadly the documentation is severely lacking and the standard programs for the described circuits are under-commented and somewhat cryptic, particularly for what is supposed to be beginner friendly product. This is the reason I decided to document what I find out and how to make the modules work.

Step 0

First you have to have setup your Pi with an OS, presumably Raspbian. Make sure everything works normally. The work will be done through command-line commands, so boot into console mode or open the terminal application. Alternatively, log into your Pi with SSH, a tool which lets you use the command-line interface comfortably from another computer. Putty is a very nice SSH client for Windows.

There are many very good guides out there in the internet, which help you with the above. This guide will not go into more details in this part.

Step 1 - Circuit

A very nice way to create the circuits is to use the provided breadboard. But first one has to understand its layout. It has spots with holes in a grid layout. These are perfect for the male connectors of the jumper cables or other electronic parts like simple diodes, resistors or the provided ADC integrated circuit.

Every numbered row has two groups of five spots. Every spot of one this groups is connected with every spot in the same group and not with any other spot on the board. Additionally, each of the four columns, two on each side, labeled + or - is also such an interconnected group.

Align your Pi such that the GPIO pins are in the top right corner. This is the way to read the Raspberry Pi Pin table found in the booklet and nearly all found in the internet. If your Pi has a different amount of rows than in a given table, the top 13 rows (26 pins) of the table and the Pi should still correspond to each other. Be aware that there are different numbering schemes for the pins. The correct one to use depends on the context. The booklet use the terms GPIO0, GPIO1 and so on; these are numbers of the wiringPi scheme.

For nearly all circuits one needs constant power from the Pi. Therefore connect the top left 3.3V + Pin of the Pi to one of the + columns. Connect a ground GND pin to a - column (in my booklet's table the GND pins are labeled 0V).

Most of the sensors have three connectors:

  • One labeled G or -. This one should be connected to the used - column.
  • One labeled S. This one should be connected directly to a GPIO pin. For the example programs to work, this has to be exactly the pin described in the booklet.
  • One which is most often unlabeled. This is +. Connect it to the + column. Make sure it is really 3.3V power and not the 5V one!

Note: When using LEDs, do not connect their GND pin directly to the - column. Although this can work acceptably, this could be bad for the diode or the Pi. Use a resistor (which is not provided in the package)! Connect one pin to the GND of the LED and other to the - column. For me a 180-Ohm-resistor has worked nicely.

Step 2 - Software

On the command line make sure you are in your home directory:

$ cd ~
$ pwd
/home/pi

Type the lines with the dollar in front. The other lines are the expected output.

Now download and unzip the example programs. The URL in the first command might change because I have no control over it:

$ wget https://www.sunfounder.com/learn/download/c2Vuc29yX2tpdF92MS4wX2Zvcl9waV9jb2RlLnppcA==/dispa -O sunfounder.zip
--2016-12-22 16:42:50--  https://www.sunfounder.com/learn/download/c2Vuc29yX2tpdF92MS4wX2Zvcl9waV9jb2RlLnppcA==/dispa

Auflösen des Hostnamen »www.sunfounder.com (www.sunfounder.com)«... 198.23.52.211
Verbindungsaufbau zu www.sunfounder.com (www.sunfounder.com)|198.23.52.211|:443... verbunden.
HTTP-Anforderung gesendet, warte auf Antwort... 200 OK
Länge: 1052555 (1,0M) [application/zip]
In »»sunfounder.zip«« speichern.

100%[======================================>] 1.052.555    541K/s   in 1,9s

2016-12-22 16:43:02 (541 KB/s) - »»sunfounder.zip«« gespeichert [1052555/1052555]
$ unzip sunfunder.zip

Obviously the exact output depends on your language settings.

If you now run ls, somewhere in the output there should be a directory sensor kit v1.0 for pi_code. Move into there with:

$ cd sensor\ kit\ v1.0\ for\ pi_code/
$ ls
Rpi_SensorKit_code  Sunfounder_SensorKit_Python_code_for_RaspberryPi-master

While typing the directory names hit the tab key. This will make the console autocomplete the name.

The directory Rpi_SensorKit_code contains the C example programs, each in its own subdirectory. The directory Sunfounder_SensorKit_Python_code_for_RaspberryPi contains the Python example programs. I recommend using the Python example programs because they work better in my experience and contain more comments.

Although it is not necessary for the Python programs, I still recommend following the booklet's instructions on installing wiringPi library. The gpio utility command, which is packaged with the library, is very handy.

Step 3 - Running the programs

Move to the directory of the Python programs with:

$ cd ~
$ cd sensor\ kit\ v1.0\ for\ pi_code/
$ cd Sunfounder_SensorKit_Python_code_for_RaspberryPi

Now, when you want to run an example program, type:

$ sudo python 01_hall_1.py

There you replace 01_hall_1.py with the name of the program you want to run. Stop the program at any time by hitting Control + c.

You can print the programs into the terminal with:

$ cat 01_hall_1.py

Again with the corresponding file name. Be aware that the Python programs use the pin numbering scheme based on the physical layout of the GPIO pins on the Raspberry Pi.