You want to measure a voltage, but it is higher than the 3.3V possible using an MCP3008 (Recipe 12.4).
Use a pair of resistors to act as a voltage divider to reduce the voltage to a suitable range. 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)
• 3.3kΩ resistor (see “Resistors and Capacitors” on page 380)
• 9V battery and clip lead
Figure 12-10 shows the arrangement for this, using a breadboard. The setup will measure the voltage of the battery.
Never use this recipe to measure high-voltage AC, or for that matter, any type of AC. It is for low-voltage DC only.
Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called adc_scaled.py.
import spidev R1 = 10000.0 R2 = 3300.0 spi = spidev.SpiDev() spi.open(0,0) def analog_read(channel): r = spi.xfer2([1, (8 + channel) << 4, 0]) adc_out = ((r[1]&3) << 8) + r[2] return adc_out reading = analog_read(0) voltage_adc = reading * 3.3 / 1024 voltage_actual = voltage_adc / (R2 / (R1 + R2)) print(“Battery Voltage=” + str(voltage_actual)) |
The program is very similar to that of Recipe 12.4. The main difference is the scaling, using the values of the two resistors. The values of these two resistors are held in the variables R1 and R2.
When you run the program, the battery voltage will be displayed:
$ sudo python adc_scaled.py Battery Voltage=8.62421875 |
Read the discussion carefully before attaching anything of higher than 9V, or you may destroy the MCP3008.
Discussion
This arrangement of resistors is called a voltage divider or sometimes a potential divider (Figure 12-11). The formula for calculating the output voltage, given the input voltage and the values of the two resistors, is:
Vout = Vin * R2 / (R1 + R2)
This means that if R1 and R2 were both the same value (say, 1kΩ) then Vout would be half of Vin.
When choosing R1 and R2, you also need to consider the current flowing through R1 and R2. This will be Vin/(R1 + R2). In the preceding example, R1 is 10kΩ and R2 is 3.3kΩ. So the current flowing will be 9V/13.3kΩ = 0.68 mA. This is low, but still enough to eventually drain the battery, so do not leave it connected all the time.
See Also
To avoid the math, you can use an online resistor calculator.
The voltage divider is also used to convert resistance to voltage when using a resistive sensor with an ADC (Recipe 12.6).
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 |