Home Technology Development BBC micro:bit Experiment — Simple Capacitance Meter

BBC micro:bit Experiment — Simple Capacitance Meter

Looking at an expansion board for the micro:bit, and using it to make a simple capacitance meter.

66
0
SHARE

Returning to the micro:bit

As much as I enjoyed experimenting with the BBC micro:bit earlier in the year, other priorities came along which meant it ended up on a shelf.

So during the Christmas break, I made time to take another look at it.

Expansion Board

Keen to experiment with hardware add-ons, I was disappointed when I realised that there are no output ports which are accessible via croc-clips — they’re all inputs. So I went ahead and ordered the expansion board from Amazon pictured below.

BBC micro:bit Expansion and Breadboard
BBC micro:bit Expansion and Breadboard purchased from Amazon, shown here without the top cover.

The expansion board enables the micro:bit to be plugged in, and provides full access to all the available inputs and outputs. It also has provision for power, but I’ve not used that facility yet, choosing instead to power the micro:bit directly as before.

A top and bottom cover was supplied with the expansion board, but I decided to fit only the bottom cover, to give better access.

Capacitance Meter

Hardware

For my first experiment, I chose to make a capacitance meter. I used two resistors which were in my junk box. Only a single GPIO port is used on the micro:bit — P8.

Capacitance Meter Circuit Layout
Capacitance Meter Circuit Layout

P8 on the micro:bit connects to the capacitor being tested via an 82R series resistor. The other side of the capacitor is connected to GND; there is also a 22k resistor in parallel with the capacitor.

If you need to purchase resistors, look for 0.25W miniature types. The values are not super-critical, and you may well have to edit the scaling value in the code (presently 1.48), to adjust the readings — especially if you use different resistor values.


ADVERTISEMENT
Advert for zazzle.com/starchip

Software

The following JavaScript code was compiled on the makecode.microbit.org website, as used in previous articles.

function getCapacitance() {
   pins.digitalWritePin(DigitalPin.P8, 1)
   basic.pause(10)
   let done = false
   let time = 0
   while (!done) {
      if (pins.digitalReadPin(DigitalPin.P8) == 0) {
         done = true
      }
      else {
         time += 1
         if (time >= 999) {
            done = true
         }
      }
      control.waitMicros(10)
   }
   return Math.floor(time * 1.48 + 0.5)
}

pins.setPull(DigitalPin.P8, PinPullMode.PullNone)
basic.forever(function () {
   basic.showNumber(getCapacitance())
})

How It Works

The P8 pin is configured as an output and set high for 10ms, to allow the capacitor to charge up via the 82R resistor.

Then, the pin is configured as an input, which causes the capacitor to discharge via the 22k resistor.

The time taken for the input to switch back to a logic zero is measured using a very simple and crude timing loop.

The measured time interval is scaled to convert it to nano Farads, then sent to the display. The scaling factor was determined when testing some sample capacitors.

For more about how the time interval is related to the capacitance, search online for RC time constant.

The discharge curve was observed using an oscilloscope, as shown below.

Capacitance Meter Waveform
Voltage across the capacitor. The falling edge shows the relatively slow discharge, which is measured by the micro:bit to determine the capacitance value.

Limitations

The use of a simple timing loop isn’t the most accurate way of measuring time intervals using a microcontroller, but it’s adequate in this case. It’s also easier to understand than other methods.

The timing loop has a fixed maximum time, to avoid the possibility of an infinite loop. This will limit the maximum capacitance value that can be measured. The code should really be altered to detect this condition, and display a suitable ‘out of range’ message.

During testing, I measured 22n, 100n and 220n components. It should be possible to extend the range, perhaps by making some code changes or resistor changes. However, there will be limits — especially at the lower end.


ADVERTISEMENT
Advert for zazzle.co.uk/starchip

Accuracy

The accuracy of the readings will be affected by a number of factors, including temperature, supply voltage, and stray capacitance. A number of improvements could be made, such as using a constant current source — but this is only meant to be a quick and simple experiment, using minimal additional components!

I/O Port Choice

Initially, I used P5 instead of P8, but found that the capacitor would not discharge sufficiently. This seems to be due to an on-board pull-up resistor. So I switched to using P8, and disabled the on-chip pull up/down resistors in code.

Final Remarks

The expansion board has proven to be a very useful micro:bit accessory, and I hope to find time to try something else before long, such as controlling a motor.

Purchase Links

MakerHawk BBC Micro:bit Expansion Board at Amazon

BBC micro:bit go (with accessories) at Amazon
BBC micro:bit (no accessories) at Amazon

(Disclosure: Using Amazon links found here helps support this site.)

Related Articles

BBC micro:bit – First Steps (Part 1)
BBC micro:bit – First Steps (Part 2)


ADVERTISEMENT
Advert for shop.spreadshirt.co.uk/starchip

LEAVE A REPLY

Please enter your comment!
Please enter your name here