You’ve tested your interface board, you’ve assembled your analogue board and now you want to try it out.
Here’s a set of quick tests that will check the board is working correctly. You’ll also learn some of the basics of the Analogue Board API.
Since you will control the PCF8591 chip used on the board using the I2C protocol, you’ll need to make sure that I2C is enabled and working on your raspbian installation. There’s advice on how to do that in Physical Python – Part 1.
Once you’ve got I2C working, the first thing to check is that the analogue board is visible on the Pi’s I2C bus.
Preparing the analogue board
Before the analogue board is connected, make sure that you have the correct jumpers in place. Here’s a picture of the board as it should be, followed by a close-up showing the jumper area:
You’ll also need to check that the board’s address is set correctly. The board can be configured to appear at one of eight different addresses on the I2C bus, ranging from hex 48 to hex 4F. Check that the switches are set for an address of hex 48, as shown below:
Connecting the analogue board
With the Pi powered down, connect your interface board to the Pi and connect the analogue board to the interface board. Your setup should look like this:
Then power up your Raspberry Pi, log in and if necessary open a terminal window.
Check that the board is detected by I2C
Now run the i2cdetect command. The arguments you use will depend on which revision of the Raspberry Pi you’re using. You should know from the I2C setup you did earlier whether your Pi uses I2C bus 0 (older boards) or bus 1 (newer boards).
In the latter case, you’d type
i2cdetect -y 1
If you have an earlier version, you’ll need to type
i2cdetect -y 0
In either case you should see output like this:
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
That shows that the analogue board has been detected at I2C address hex 48. That’s the default for the board.
Next you’ll check that you have the quick2wire software installed correctly, and after that you will do some simple tests to verify that the board is working.
Checking the Quick2Wire software installation
In a command window, start python3 by typing
at the command prompt.
You should see text like the following:
You’re now running version 3 of python in interactive mode. In other words, python will execute expressions as you type them, so you will get immediate feedback if python encounters a problem.
At the >>> prompt, type
from quick2wire.i2c import I2CMaster
and press enter.
If you see an error message like ‘ImportError: No module named quick2wire’, there’s a problem with the software installation which you will need to correct before you can continue.
If all goes well, python will just display another line of >>> symbols. The screen should look like this:
If your window looks like that, you’re ready to carry out your test. That will require a bit of wire and a bit of Python code! But first, exit from the python3 session by typing
You should see an ordinary command prompt:
Hooking up the board
The first test you’ll do is very simple. You will run an example which prints the voltage detected at one of the board’s four analogue inputs. These are known as AIN0, AIN1, AIN2 and AIN3. You can see the names of the input printed on the analogue board. The input we’ll use for the test is the one known as AIN0.
If you try to measure the voltage on an unconnected input the value will be unpredictable. To make the test meaningful, you’ll run code that repeatedly displays the voltage, and you’ll connect the input to a known voltage, first to ground (zero volts) and then to the 3.3 volt supply.
Here’s a picture of the board with AIN0 connected by a jumper wire to ground:
Running the example
You should still have a command window open, displaying a command prompt. You’ll be running one of our standard examples, and the location of that code will depend on how you installed our library. Fortunately you can use the find command to determine its location. In a command window, type
find /usr /home -name pcf8591read
That will look for all the files called pcf8591read in the directories under /usr and /home. Since there is almost certainly just one such file, the command will probably print a single line showing the path to the file.
- If you installed our software from github, you’ll probably see a line like /home/pi/git/quick2wire-python-api/examples/pcf8591read.
- If you installed the software directly from our debian repository, you will see a line like /usr/share/doc/quick2wire-python3-api/examples/pcf8591read
Since the line returned by find is the full path to the example program, you could manually copy the entire line and linux would run the example program. That’s a boring and error-prone way of doing things, and there’s a useful shortcut. If you type `find /usr /home -name pcf8591read` then linux will execute the find command and then execute the result as if you had typed it yourself.
Be careful – the characters around the find command are back-ticks, not quotation marks. On my keyboard, the back-tick is at the top left of the keyboard, but it may be somewhere else on yours. Just make sure you use the backward-sloping character and not a single or double quote.
If all goes well, you’ll see output like this:
Stop the program by typing control-C (In other words, hold down the control key and type C, then release the control key).
Python will print out a message showing that it has responded to your keyboard interrupt, and the example will stop running.
Now you need to connect AIN0 to the 3.3 volt supply, and then run the example again.
Connecting the input to 3.3 volts
Unplug the jump wire from the socket marked ground and insert it into the socket marked VREF. It should look like this
Now run the example again, and prepare for a surprise. (You don’t have to retype the expression you used before – just press the up-arrow key once on your keyboard to recall what you typed before, and then press enter to re-execute it).
This time you should see output like this:
You might have expected to see a value of 3.3 – after all, you just connected the board’s input to a 3.3 volt supply. But the board does not know what voltage it’s being run at; all it knows is that it’s seeing the maximum possible voltage at the input, and the software represents that as a value of 1.0. If you wanted to show the actual voltage, you’d need to multiply the output by 3.3
If the two sets of output are as expected, you’ve verified that analogue input 0 is working as it should. In the next part of this article, we’ll modify the example code to check the remaining inputs, and also verify that the analogue output is working correctly.