Microcomputer tricks

Voltage input with Raspberry Pi

This chapter includes short and simple instructions how to do voltage measurements with Raspberry Pi for hobbyist with limited knowledge of electronics.

Arguably the biggest disadvantage of Raspberry Pi is that, unlike its cousin Arduino, it does not have proper analogue inputs. That means that all GPIO (general purpose input output) pins are purely digital, so in principle they only supply/recognise two voltages, 0.0V for False state and 3.3V for True state. If you want to read an arbitrary voltage, you need to use ADC (analogue digital converter) circuit and connect it to several GPIO pins.

[ADC Differential Pi] [ADC Pi Plus] [ADS1115]

Picking an ADC microprocessor chip, installing and programming it can be quite a feat, especially for a hobbyist. Fortunately, there are few ready commercial solutions for that purpose, with detailed instructions, program libraries and examples:

All these devices connect to I2C channel of the Raspberry Pi, where AB Electronics devices use two programmable and Adafruit device uses one programmable address. This is a plus, since all your GPIO pins remain free for other uses. All devices also have internal voltage reference, which makes voltage measurement very precise and independent on Raspberry Pi. The basic differences can be seen in the table:

NameVoltage (V)InputsMax. precisionMax. samples per secondImpedance (Ω)Addresses
ADC Differential Pi-2.048 to +2.0488 differential18 bit2402.25M/25M0x68 to 0x6F
ADC Pi Plus0.0 to +5.08 single ended18 bit24016.8k0x68 to 0x6F
ADS1115-3.3 to +3.32 differential16 bit86015M0x48 to 0x4B
0.0 to +3.34 single ended16 bit8606M0x48 to 0x4B

Unless you intend to mimic oscilloscope, the sampling speed is rather unimportant (and in that case you wouldn't use slow I2C channel anyway). Precisions are also comparable and satisfactory for all devices. The essential differences therefore are:

If you want to play safe, there is a relatively simple solution: you can buy ADC Differential Pi or ADS1115 and build a small simple voltage divider yourself. This is explained in the next section.

Building a simple and compact voltage divider

This chapter includes short and simple instructions how to build a simple and compact four channel voltage divider for hobbyist with limited knowledge of electronics.

[principle]

We have to start with a principle of a voltage divider. This are essentially two resistors R1 and R2 in series attached to the source voltage Vin, as shown on the picture above. Voltmeter is attached to the second resistor R2 only. Since voltmeter internal resistance is very large, much larger than resistances R1 and R2, we can safely assume that the current through voltmeter IV equals zero. Then the current through both resistors I and voltage on the second resistor and voltmeter Vout can be easily evaluated.

Resistances must be carefully picked and should be much smaller than voltage internal resistance, but still as large as possible. For our needs values around 10kΩ are appropriate. Knowing the maximum source voltage and maximum voltmeter voltage, one can pick a suitable pair of resistors to do the voltage transfer. Note that widely available resistors come in discrete values, typically 1kΩ, 1.2kΩ, 1.5kΩ, 1.8kΩ, 2.2kΩ, 2.7kΩ, 3.3kΩ, 3.9kΩ, 4.7kΩ, 5.6kΩ, 6.8kΩ, 8.2kΩ, 10.0kΩ, etc.

Suppose that we want to transfer 5.0V to 2.048V, which is maximum voltage of ADC Differential Pi. Some of the suitable combinations are R1=10.0kΩ R2=6.8kΩ (used by ADC Pi Plus) and R1=6.8kΩ R2=4.7kΩ. In my demonstration below, I use the second combination.

[resources]

In order to build four channel voltage divider, we need prototype board with copper stripes, breakable male pin row and at least eight resistors of power 1/8W and tolerance 1%, four of resistance R1 and four of resistance R2. Tolerance of 1% is essential to get a reliable voltage value, while still being very cheap and easy to obtain. The final result can be improved by buying ten resistors of each value and pick four having resistivity closest to the average value.

[grahphic] [front side] [back side]

In order to create a compact four channel voltage divider, we cut out a board with seven stripes each containing ten holes, as shown on the left picture. Resistance of horizontal resistors is R1, while resistance of vertical resistors is R2. Connections under R1 resistors should be broken by scratching the copper stripe, as shown on the right picture. Top left pins can be connected to positive poles of four input source voltages Vin, while top right pins are connected to positive poles of four voltmeter voltages Vout. Bottom left connectors represent the ground and are connected to negative poles of source and voltmeter voltages.

Voltage output with Raspberry Pi

This chapter includes short and simple instructions how to do voltage output with Raspberry Pi for hobbyist with limited knowledge of electronics.

Neither Raspberry Pi nor Arduino have proper analogue outputs. Actually, both microprocessors have few GPIO (general purpose input output) pins with PWM (pulse-width modulation) output. By switching between two digital states, False state at 0.0V and True state at 3.3V, GPIO simulates mid-voltages. This is however rather imprecise and useless for use in precise electronic devices. If you want to write an arbitrary voltage, you need to use DAC (digital analogue converter) circuit and connect it to several GPIO pins.

[MCP4725]

Picking an DAC microprocessor chip, installing and programming it can be quite a feat, especially for a hobbyist. Fortunately, there is one ready commercial solutions for that purpose, with detailed instructions, program libraries and examples:

What I don't like about this solution is that it only provides one voltage output and has fixed I2C address 0xC0. There are however many alternatives. For example, Microchip also offers MCP4728, which has four voltage outputs, while I2C address can be programmed between 0xC0 and 0xC7. Additional plus is that the chip is really cheap!

[resources]

In order to prepare our four-channel DAC, we need the MCP4728 chip, a DIP adapter PCB board, a single row male pin header connector (I strongly advise to use round pins), three 1.8kΩ resistors (anything up to 10kΩ will also do) and a solder wick. The biggest challenge is to solder the chip to the PCB board: The chip is extremely small, with total dimension of 3mm × 3mm, and the distance between pins of only 0.5mm! The task is difficult to do, but it is doable.

[soldered chip]

I succeeded in soldering three chips using the amateur soldering iron and solder wire and the result is on the picture above. I used a good solder paste to flood the PCB board before soldering and a good solder wick to remove superfluous solder afterwards. There are dozens of instructions on the web how to do that, for example general directions for surface mounting soldering or soldering similar chip to an adapter PCB board.

Currently there is no instructions or library for this chip at the moment, so I decided to put some effort in the matter and create a simple library myself. Please find:

[writing voltages]

Picture above shows the setup for writing voltages. There are no pull-up resistors, as the setup uses Rasberry's default I2C pins with built-in pull-up resistors. The capacitor is optional, but highly advisable.

[address reading and changing]

Picture above shows the setup for reading and changing I2C address. This procedure requires I2C communication together with controlling LDAC register. Since this is not a task to be done often, I advice that three non-I2C GPIOs are used, so we have to put a pull-up resistor for each of them.

Please note: There are serious bugs in the MCP4728 manual from 08/04/10:

I lost two days head banging to figure that out!

Current library is very rudimentary. However, if you want to use other options, described in MCP4728 manual, please write me and I will extend it.

Please report what else would you like to see here, suggestions, new tricks etc. to the author using feedback form.

Created by Marko Pinteric: feedback form.

Updated . Web page has been read by visitors since February 2017.

Valid XHTML 1.0! Valid CSS!