You have a resistive sensor that you wish to use with an MCP3008 ADC chip.
Use a potential divider with one fixed resistor and the resistive sensor to convert the resistance of the sensor into a voltage that can be measured with the ADC.
As an example, you can remake the light sensing Recipe 12.2 to use the MCP3008 instead of the step response technique.
To try this recipe, you will need:
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• MCP3008 eight-channel ADC IC (see “Integrated Circuits” on page 381)
• 10kΩ resistor (see “Resistors and Capacitors” on page 380)
• Photoresistor (see “Resistors and Capacitors” on page 380)
Figure 12-12 shows the arrangement for this, using a breadboard.
You can use the exact same program as Recipe 12.4 (adc_test.py). Covering up the light sensor with your hand changes the readings. You also need to set up SPI on your Raspberry Pi, so if you haven’t already done so, follow Recipe 8.6.
$ sudo python adc_test.py Reading=341 Voltage=1.098926 Reading=342 Voltage=1.102148 Reading=227 Voltage=0.731543 Reading=81 Voltage=0.261035 Reading=86 Voltage=0.277148 |
These readings may be quite a bit different, depending on your photoresistor, but the important thing is that the figure changes as the light level changes.
Discussion
Choice of fixed value is not very critical. If the value is too high or low, then you will just find that the range of readings is rather narrow. Select a resistor value somewhere between the minimum and maximum resistance of the sensor. You may need to experiment with a few resistors before deciding on one that suits your sensor over the range of readings that you’re interested in. If in doubt, start with 10kΩ and see how that works.
You can swap out the photoresistor for pretty much any resistive sensor. So, for instance, you could use the gas sensor of Recipe 12.3.
See Also
To measure light intensity without the complication of an ADC, see Recipe 12.2. For an example of using more than one channel of the ADC at a time, see Recipe 12.8.
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-6. Integrated circuits | |
7805 voltage regulator | SparkFun: COM-00107 |
L293D motor driver | SparkFun: COM-00315, Adafruit: 807 |
ULN2803 Darlington driver IC | SparkFun: COM-00312, Adafruit: 970 |
DS18B20 temperature sensor | SparkFun: SEN-00245, Adafruit: 374 |
MCP3008 eight-channel ADC IC | Adafruit: 856 |
TMP36 temperature sensor | SparkFun: SEN-10988, Adafruit: 165 |
Table A-4. Resistors and capacitors | |
270Ω 0.25W resistor | Mouser: 293-270-RC |
470Ω 0.25W resistor | Mouser: 293-470-RC |
1kΩ 0.25W resistor | Mouser: 293-1k-RC |
3.3kΩ 0.25W resistor | Mouser: 293-3.3k-RC |
4.7kΩ 0.25W resistor | Mouser: 293-4.7k-RC |
10 kΩ trimpot | Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF |
Photoresistor | Adafruit: 161, SparkFun: SEN-09088 |
220nF capacitor | MCM: 31-0610, Mouser: 80-C322C224M5U5HA |