AmbiLamp: Hardware
Testing Each Device

We present the IoT AmbiLamp PCB schematic for your reference:

With our Raspberry Pi configured and the AmbiLamp PCB soldered with our sensors and actuators. We are ready to start bringing the AmbiLamp together.

With the Raspberry Pi powered off, connect the Raspberry Pi and the AmbiLamp PCB together.

If you have not already connected the LED, OLED screen, or Serial Cable, go ahead and connect them now.

Be aware of the LED orientation:

The serial cable connection:

Digikey Serial Cable: BLUE is GND; GREEN is CABLE RX (RPI TX); RED is CABLE TX (RPI RX);

Adafruit Serial Cable: BLACK is GND; WHITE is CABLE RX (RPI TX); GREEN is CABLE TX (RPI RX);

The full assembly, without the serial cable attached:

Careful not to short-circuit the LDRs if you have them extended off the board.

Power the Raspberry Pi and log into the Raspberry Pi.

The RGB LED may turn on fully briefly or until we run some code; otherwise, it will stay on, but dimmed.

On the pi, we are borrowing the Adafruit's DHT22 library to use our DHT22 on the AmbiLamp PCB. In order to use the Adafruit DHT22 library we need to install it.

Clone the Adafruit DHT22 Python Library:

$ git clone

Change into the Adafruit DHT22 Python Library directory:

$ cd Adafruit_Python_DHT/

The contents of the Adafruit DHT22 Python Library:

Here, we will install the library so we can use it with our wrapper libraries. To install the Adafruit DHT22 Python Library:

$ sudo python install

After the installation is complete. There is now a Python module installed in your Python environment. We could use Adafruit's DHT22 Python Library, however the libraries we have made are wrappers of this library. So we will now get rid of this directory.

First we back out of the directory:

$ cd ..

Then to remove the directory:

$ sudo rm -rf Adafruit_Python_DHT/

Now, we will bring our libraries. If you have not already cloned the IoT AmbiLamp starter repository, clone it now:

$ git clone

The contents of the Iot AmbiLamp starter repo:

In the mcad folder, we have the IoT AmbiLamp 3D CADing materials. In the code folder, the code that we will use to interface with the hardware connected to the IoT AmbiLamp PCB.

With the Adafruit DHT22 Python Library installed, all the hardware should be ready to go. However, we need to turn on a few of the Interfacing Options in the raspi-config.

We need to enable the following Interfacing Options:


Refer to our Raspberry Pi Setup: Module 09 [More] Raspberry Pi Configuration: raspi-config and enable SPI and I2C.

Finally, plug in the OLED screen - the picture below shows the pinout on the AmbiLamp PCB.

We now have everything ready to interface the Raspberry Pi to interface with the sensors and actuators of our IoT AmbiLamp PCB.

In the IoT AmbiLamp repository that we cloned, navigate to the code folder:

$ cd iot_ambilamp/code/

The contents of this folder should be similar to the following:

In this directory you will write the necessary code to interface with the hardware. We have several examples to demonstrate how some of these devices work. Inside the libraries folder, there are the driver modules for the hardware. In addition, there are more test scripts.

The content of the libraries folder:

We encourage you to explore these libraries. If you are comfortable with Python, you may modify the libraries as you see fit or change the structure.

Let us test each piece of hardware to make sure they are working as intended.


To test the DHT22, we navigate to the DHT22 folder:

$ cd iot_ambilamp/code/libraries/DHT22

Execute the test script provided:

$ sudo python

After a few seconds, if the Adafruit DHT22 Library was installed properly, it will return something similar to the following:

The first value is the humidity that the sensor measures. The second value is the temperature (in Celsius) that the sensor measures.

OLED Display (SSD1306)

To test the OLED, we navigate to the SSD1306 folder:

$ cd iot_ambilamp/code/libraries/SSD1306

There are several test scripts made by Adafruit. Feel free to test each one of them and study them to see some of the capabilities of the OLED display.

We demonstrate the execution of the shapes test script:

$ sudo python

After a moment, the OLED screen will display the following:

If you see the stretched shapes of a circle/ellipse, square/rectangle, triangle, and X, and the Izzy and Kuro text, then the OLED screen is working properly.

Izzy and Kuro are the name of our kittens :3


To test the Common-Anode RGB LED, we navigate to the RGB_LED folder:

$ cd iot_ambilamp/code/libraries/RGB_LED

Execute the test script provided:

$ sudo python

The LED will cycle through various brightnesses of red, blue, and green.


The Raspberry Pi does not have any analog sensors like many microcontrollers (e.g. Arduino). However, we can use the MCP3008, which is a multi-channel ADC (Analog-to-Digital Converter). The MCP3008 communicates to the Raspberry Pi using the Serial Peripheral Interface (SPI).

The MCP3008 has 8 channels. We are utilizing 6 of these channels. 2 of them are being used with the Sparkfun Sound Detector. 4 of them are being used for each of the (4) LDR (photo resistors). The remaining 2 channels are grounded.

To test the MCP3008, we navigate to the MCP300X folder:

$ cd iot_ambilamp/code/libraries/MCP300X

Execute the test script provided:

$ sudo python

Observe the flood of analog sensor readings from the 8 channels. Channel 6 and 7 should be zero (or very small numbers), denoting that those pins are grounded.

Our channel 1 readings are pretty bad, we may have broken our LDR or the pin on the MCP3008.

Channels 0, 1, 2, and 3 are each of the LDRs. You should be able to wave your hand over the sensors and observe their values changing. The LDRs measure intensity of the ambient light around it.

Channels 4 and 5 are the Sparkfun Sound Detector. This sensor is discussed next.

Sound Detector

The Sparkfun Sound Detector has several capabilities jam packed into one device. If you observe the schematic the device is powered with 5V - it cannot be powered with 3.3V and be expected to work properly. The Raspberry Pi I/O pins only output 3.3V and can only receive 3.3V. The output of the Sound Detector pins are all at 5V. The Raspberry Pi cannot take the 3.3V, so we employ voltage dividers on all the output pins of the Sound Detector into the Raspberry Pi and the MCP3008.

Observe from the schematic. The GATE pin of the Sound Detector is into one of the Raspberry Pi digital pins. The GATE is a digital pin. The ENVELOPE and AUDIO pins are analog pins. The ENVELOPE pin is suppose to represent the envelope or the peak of the sound/noise of the environment. The AUDIO pin is suppose to mimic the true analog waveform of the sound/noise in the environment.

Because the ENVELOPE and AUDIO pins are analog pins, we are using the MCP3008 to read from the Sound Detector.

To test the Sound Detector, we do not navigate to the Sound Detector folder (because we need access to the MCP3008 folder) folder, but to the iot_ambilamp/code folder:

$ cd iot_ambilamp/code

Execute the test script provided:

$ sudo python

Observe the flood of Sound Detector readings from the the 3 different outputs.

The first output is the GATE, the second is the ENVELOPE, and the third is the AUDIO. In the image shown, observe the GATE is 0 (quiet), and then I clapped, the GATE threshold was overcome and shown a digital 1 (loud). Notice the ENVELOPE did a pretty good job of recognizing the first reading of the clap with a (relatively) large ampltiude. As for the AUDIO, well... we have never been able to do something very meaningful, let us know if you have better luck!

There is one last test script we created that combines some of the sensors. This is just an example that used several of the other examples in the code provided.

To run this test script, navigate to the iot_ambilamp/code folder:

$ cd iot_ambilamp/code

Execute the test script provided:

$ sudo python

Observe on the OLED Display the following:

This script displays the IP address of your IoT AmbiLamp, the temperature and humitiy reading from the DHT22, and the brightness or analog reading from one of the LDRs.

When writing your own code to make your IoT AmbiLamp functon, you will want to code in the iot_ambilamp/code folder. This way you have access to all the libraries to interface with and drive the hardware on the IoT AmbiLamp PCB. You are welcome to change the structure of the libraries as you see fit - they are by no means perfect. Feel free to adapt the several examples provided!