Practical Wireless

An Arduino CR Meter

Tony Jones G7ETW offers a design for an Arduinobas­ed CR meter that brings together hardware and software in a useful piece of test equipment.

- Tony Jones G7ETW Charles.jones125@yahoo.co.uk

Tony Jones G7ETW offers a design for an Arduinobas­ed CR meter that brings together hardware and software in a useful piece of test equipment.

This article has been light relief for me. It’s January, and I’ve recently written three theory-heavy pieces. So, I offer you an Arduino project, a practical mix of simple electronic­s and C programmin­g. (In the text I refer to two versions of the code, but only one, V5.1, is printed and is on my website. That’s not a mistake; readers can switch between versions by changing one line of code.)

The Hardware

An Arduino Uno R3 is the smallest fully ‘pinned’ Arduino available, with the least memory. Because of that, this project will work on any Arduino board.

The display is a 1602 I2C Serial LCD 4 (row) by 20 (character) type. This bigger display makes for a richer user-interface than 16 by 2 models allow. I prefer I2Cconnect­ivity because it requires four pins compared to seven for an SPI display.

Fig. 1 shows the breadboard circuitry. Resistance and capacitanc­e measuremen­ts rely on a potential divider, where I measure the voltage across an unknown component. Please ignore points A and B for now. The code is on my website, called ARDLCSV5.1.

Measuring Resistance

An A-to-D divides a fixed, DC voltage between a fixed number of steps. On an Arduino, 5V is divided into 1023 (two to the power of 10, minus 1) steps. Ohm’s Law does the rest, making one resistance ‘quantum’ = (Rfixed + Rtest)/1023.

You see the problem. As Rfixed + Rtest increases, one resistance step increases and the ability to tell two values apart decreases.

When Rtest is small compared to Rfixed, the resolution is effectivel­y set by Rfixed. But as Rtest rises, the balance changes and by the time Rtest is 10 times R the resolution has dropped significan­tly.fixed

Please start off with V4.1 – this is the code, with gVersion = 4.1. Rfixed is 1kΩ. This value is a compromise. It can’t be too low because of an Arduino’s 40mA maximum current limit for a pin.

ARDLCSV5.1: Software and Hardware Improvemen­ts

The problem with V4.1 is that Rfixed is just that − a fixed single resistor − and this is rather a blunt instrument. Imagine I’m trying to measure an exactly 220kΩ resistor. R fixed is 1kΩ.

A to D integer reading (assuming downward quantisati­on) from pin A3 = 1023 × (220kΩ/221kΩ), which is 1018.

1kΩ is small compared to hundreds of kilohms, so a wide range of Rtest values (204kΩ to 255kΩ in fact) will give this same 1018 read value. One fixed resistor is not good enough. (I never thought it would be, really.)

Table 1 shows some results I got with V4.1. I based the calculatio­ns above on 5V but look at the voltages! For this method to be accurate, I’d need the reference voltage to be a lot more stable than this!

Fig. 2 shows how to incorporat­e a relay board in the project. The relay contacts, connected together as shown, form an additional variable resistor between points A and B on Fig. 1. (The relay board also needs a 5V and ground connection, but these aren’t shown.)

Please switch versions to V5.1 by changing gVersion to 5.1. This version manages the relay. Now, for a 220kΩ resistor, this (same) reading of 1018 based on the same 1kΩ resistor exceeds an ‘auto-range required’ trigger and the code, seeing this, switches in an additional 10kΩ, increasing Rfixed to 11kΩ.

A second reading gives 1023 × (220k/231k) which is 974 – still over my trigger value, but better. An additional 100kΩ is switched in. Rfixed is now 111kΩ and a third reading gives 1023 × (220k/331k), which is 679, nicely in the middle. Rtest can be (acceptably) accurately calculated.

These relay boards work differentl­y to

‘traditiona­l’ stand-alone units. To make a relay switch, its input must be grounded – look at the high and low settings for relays 1 and 2. See Sidebar: ‘Relay Boards’ for more informatio­n.

Fig. 3 shows the breadboard layout (for V5.1, although it’s almost the same for V4.1).

Table 2 also shows some results I got with V5.1. This change makes quite a difference with large values of Rtest.

Measuring Capacitanc­e

Measuring capacitanc­e depends on Rcap (another fixed value) and a millisecon­d timer. 1MΩ slows the charging of nano- and micro-Farad capacitanc­es sufficient­ly to measure their time constant (tau) accurately, but pico-Farad capacitors charge too quickly to be timed.

This speed-governing cuts both ways. What charges slowly, discharges slowly and the first time I measured a 470wF capacitor I realised what was happening and used the time to unload the dishwasher! I added the LED to indicate that dischargin­g was in progress after that.

I don’t measure a whole time constant by the way. I did some maths – see Sidebar:

‘Capacitor Charging’ − and found a way to speed things up. See Table 3 for some test measuremen­t results. These were done with V4.1, but for capacitors the two versions are identical.

Coding Aspects

This is not a programmin­g magazine, but I’d like to highlight some of the programmin­g tricks I used.

String Functions but no Strings

All the literals are character arrays. I didn’t use string objects because they are memory hungry. But I did use Arduino’s standard string manipulati­on functions, which manipulate character arrays, because these improve readabilit­y.

Compiler Substituti­ons

An Arduino UNO R3 has 2kB for variables. That’s not a typo – I really do mean 2048 bytes.

Hence my use of define statements, which are labelled pieces of fixed text. When the sketch is ‘parsed’ (read for compiling), each of these is substitute­d. The compiler does not see ‘LEDPIN’; it sees ‘12’ instead. This saves space for variables that do need to change.

Real Programmer­s do use GOTO

Sometimes − not often, I admit − a GOTO is good coding. (I look forward to the Letters page!)

In this case I needed three analogRead blocks. I could have used Rfixed to determine the logic path, or even a Do-loop with an indexed array of resistor values, but it was just simpler (and more fun!) to have a GOTO.

The Empty Loop

A classic while loop is coded as While (condition) Do actions. The logic is: Perform the test

If and ONLY if the test evaluates TRUE, follow a series of instructio­ns

Go round again

I needed analogRead­s to repeat while the capacitor discharged. I could have coded this as:

int X = analogRead(A2);

While (X > CAPMIN) {X = analogRead(A2);}

This would have worked, but it needed a variable and it’s bulky. While (analogRead(A2) > CAPMIN) {} may look odd, it’s brutally efficient.

Conclusion – and an Apology

When I started on this, I intended to make an LCR meter.

Just as Capacitanc­e opposes the change of voltage, storing energy in an electric field, Inductance opposes the change of current, storing energy in a magnetic field. And inductors too have a time constant, equal to L/R.

I did some more modelling. To measure L this way (measuring an exponentia­llychangin­g voltage in a potential divider) is possible, but it’s not easy. Rfixed would have to be tiny − a few Ohms perhaps − or the time constant would be large. And Rfixed being small brings me back to an Arduino pin’s 40mA limit.

There are ways though, predicated on making oscillator­s (or at least tank circuits) and measuring frequencie­s and/or reactances. Such a design would work just as well for a capacitor of course, and I do plan to revisit this.

In the meantime, I hope this is of interest. As can be seen from the results, I don’t plan to take my plans for the Arduinomet­er to the Dragons any time soon, but many (cheap) DMMs don’t have capacitanc­e ranges so this is, in extremis, of some use.

This was an interestin­g project and demonstrat­es why 10-bit A-to-Ds (and Arduinos) have their limitation­s. But if nothing else, it was fun!

 ??  ??
 ??  ??
 ??  ??
 ??  ?? Fig. 1: The breadboard circuitry.
Fig. 2: incorporat­ing a relay board.
Fig. 3: View of the breadboard layout.
Fig. 4: Charging of a capacitor.
Fig. 5: Simplified diagram for one-relay board.
Fig. 1: The breadboard circuitry. Fig. 2: incorporat­ing a relay board. Fig. 3: View of the breadboard layout. Fig. 4: Charging of a capacitor. Fig. 5: Simplified diagram for one-relay board.
 ??  ?? Table 2: Resistance Test Measuremen­ts in Ohms (with autorangin­g)
Table 2: Resistance Test Measuremen­ts in Ohms (with autorangin­g)
 ??  ?? Table 3: Capacitanc­e Test Measuremen­ts
Table 3: Capacitanc­e Test Measuremen­ts
 ??  ??
 ??  ??

Newspapers in English

Newspapers from United Kingdom