Connecting Analogue Sensors to your Raspberry Pi Using Cayenne
Following from the previous (see here) tutorial detailing how to connect an LED to your Raspberry Pi using Cayenne from myDevices, the next step is to begin to use the platform's true functionality by connecting both light and temperature sensors to the system, that can trigger a range of alerts depending on the situation! Exciting stuff!
What you'll need:
- Raspberry Pi and Computer/Smartphone set up with Cayenne from myDevices. (Installation instructions here)
- MCP3008 ADC IC (Buy one here)
- 2x 10kΩ Resistors
- Light Dependant Resistor (LDR)
- Pi T-Cobbler (Optional) (Find one here)
- Full Sized Breadboard (Find out more here, and recap how they work)
- 17x Male to Male Jumper Wires
As the T-Cobbler is simply a way of connecting all of the Pi's GPIO pins to a breadboard, this is completely unneccessary, however it makes it a lot easier to connect the components, and as the system gets more complex with other components, it becomes more and more useful. Without a T-Cobbler, 6 male to female jumper leads can be used to connect the individual GPIO pins to the breadboard for use with the components.
An ADC (Analogue to Digital Converter) is needed as the two sensors can return an infinite number of values (based on their variable resistance) and the Raspberry Pi is unable to read this values as it is only capable of digital input. Therefore, the IC is used to convert the Analogue signals to a digital (1s and 0s)waveform that can be understood by the Pi.
HOW TO CONNECT AN Analouge Sensors TO CAYENNE
- Connect your Pi Cobbler to your breadboard.
- Attach the MCP3008 IC to the breadboard as shown in the first diagram on the right, ensuring that the notch on the top of the chip is facing the Pi cobbler, and the circular dent is in the bottom left corner.
- Using the male to male jumper leads, connect the IC to the cobbler, using the connections shown in the diagram on the right.
- You may have noticed that the bottom half of the MCP3008 IC has got nothing connected to it. This is because the eight pins at the bottom of the chip are all dedicated to a different channel, meaning that the chip can connect up to eight different analogue sensors to your Pi!
- Connect the Thermistor to the ADC as shown in the second diagram on the right, using one of the 10kΩ resistors. The diagram shows the Thermistor being connected to channel 0 on the ADC.
- Next, connect the LDR to the ADC, much like how you connected the Thermistor. The third diagram shows it being connected to the ACD's channel 0, however as this is being used by the Thermistor, this should be connected to channel 1, the next pin to the right.
- All connected! Well done -make sure that every connection is identical to those in the circuit diagrams - even the smallest difference could cause problems! Next, it's software time!
- Log onto Cayenne on your desktop computer, and ensure that your Pi is powered on and connected to the system, as shown here.
- Navigate to the 'Add new...' menu in the top left of the screen, then click on 'Device/Widget'. Your screen should look like the screenshot on the right.
- Select the 'Analogue Converters' option, then click on the MCP3008 icon, as shown on the right.
- Finally, ensuring that the 'Choose Chip' option is set to 'SPI Chip-Select 0', click the 'Add Extension' button to add the ADC to the Raspberry Pi.
- The next step is to add the two sensors two the system under the control of the ADC. This can be done by navigating to the 'Add new...' menu in the top left of the screen, and then by clicking on 'Device/Widget'.
- Select 'Temperature' from the top of the page, followed by 'Thermistor', the third option down.
- Fill in the boxes, selecting the MCP3008 in the 'Connectivity' box and Channel 0 in the 'Channel' box. You can also chose the widget and icon of your choice, to be displayed on your dashboard. Click 'Add Sensor', the first readings from the Thermistor should appear on the dashboard!
- Next, to add the LDR, repeat step 5, then select 'Luminosity' followed by 'Photoresistor'. Fill in the boxes in the same way as before, with the only difference being selecting channel 1 in the 'Channel' box. Click 'Add Sensor' and the LDR will begin to show readings on your dashboard.
Congratulations, you now have two analogue sensors displaying their values on your dashboard, which can be accessed from anywhere in the word! But their's more! Follow these instructions to get alerts when the sensors read certain values:
- From the dashboard, Navigate to the 'Add new...' menu in the top left of the screen, then click on 'Trigger'.
- On the next screen, select either the Raspberry Pi from the list on the left, and drag it to the box named 'If'. Then, in the drop down menu that apprears, slelect either the thermistor or the photoresistor. The slider can then be adjusted to a desired value and either the option 'Value Above' or 'Value Below' selected.
- Finally, choose your preferred result of the aforementioned trigger, which can be turning on/off an actuator already connected to the system, or by sending a notification to one or more people via email and/or text message! This can be done by either dragging your Raspberry Pi from the list on the left, and then selecting the device from the drop down menu as before, or by selecting the 'setup notification' button. Once setup, select 'Save Trigger' in the bottom right-hand side of the screen.
- The next page will display all the triggers that have been setup. Here you can enable/disable them as you please, as well as editing the constraints as you see fit.
All set! The fantastic thing about this software is that it is so customisable to fit easily into any situation, you can just keep changing it until it meets your requirements! All that's left to do is put your new setup somewhere where it is useful - I put mine in our greenhouse so that my Dad (the gardener!) gets a text message when it gets too hot, so he can go and open the window vents, saving the tomatoes from being fried!
Don't forget to create your account and get setup on Cayenne from myDevices by clicking here.