Making a Do it Yourself Ground Pin


When I’m making embedded devices, I find that ground pins are a bit like liquorice allsorts. I just can’t get enough of them. The ground pin is the one that provides an electrical ground connection. You need a ground connection for the power for every device you want to add to your circuit, and also for every data signal. If you want to provide a push button for your user to press, yes, that needs a ground connection along with the connection to the input pin that is going to read it.

The Arduino has a few ground pins in amongst its selection of inputs and outputs. The Wemos D1 Mini that I like to use only has one ground pin. It’s really annoying when you’ve got lots of spare input and output pins on your device, but you’ve run out of ground pins. It’s a bit like “water water everywhere, but not a drop to drink…”

Anyhoo, there is a piece of programming magic that you can use to convert any digital output pin into a ground pin. Actually it’s not that magical. You just have to set one of your digital pins to be an output pin and set the level to 0. Hey presto. Instant ground pin.

You can’t really use such a pin for power connections, but you can use it for data connections. I’ve just used it on my MQTT Mini air quality device. It needs some form of input and I’m determined to avoid putting buttons on it. So I’ve added a tilt sensor. This the software to react when the user turns the sensor upside down.

The tilt sensor works like a switch. One way up a small metal ball rests on two pins, providing an electrical connection. Tip the sensor the other way up and the ball falls down away from the pins, breaking the connection. I wanted to add it to my Wemos device and, of course, I had no ground pins left. So I used a data pin instead. This is the code that makes the switch work:

#define GROUND_PIN 5
#define INPUT_PIN 4

digitalWrite(GROUND_PIN, 0);

I’m using data pins 4 and 5. Pin 5 is my “do it yourself ground”. Pin 4 is my input pin. The tilt switch is wired across these two pins. Pin 5 is configured as an output pin and set to the low (ground) level. Pin 4 is configured as an input pin with a “pull-up” resistor which means that when nothing is connected to the pin the data signal on the pin is pulled into the high state. When the switch closes, this pulls the voltage on pin 4 down to the level of pin 5 (ground) causing the input that the program sees to change from 1 to 0. My code to test the pin looks like this:

if (digitalRead(INPUT_PIN))
// device is upright
// device is upside down

It works a treat. So, if you need a ground line for data and you’ve no ground connections - but you have some spare signals - just make a ground of your own.