Output example with LED

In this example, we will turn on a Red LED programmatically. Let’s take a look at the below circuit diagram.

(Simple LED Output)

From the above circuit diagram, we have connected Pin no. 6 (ground pin) to the negative (ground) rail of the breadboard and BCM 4 to the one end of a 1k ohm resistor. The other end of the resistor is connected to the input of a Red LED and the output of the LED drains to the ground.

There is nothing interesting about this circuit except the resistor. The resistor is needed because Red LED operates at 2.4V and a GPIO pin provides 3.3V which can damage the LED. Also, the LED draws 20mA which is above the safe limit of Raspberry Pi, hence resistor will also prevent the excess current.

We can choose between 330 ohms to 1k ohm resistance. This will impact the current flow but won’t damage the LED.

From the above circuit, the only variable in the circuit is BCM 4 pin output. If the pin is on (3.3V), the circuit will close and LED will glow. If the pin is off (0V), the circuit is open and LED won’t glow.

Let’s write a program that can programmatically turn on the BCM 4 pin.

In the above program, we are importing onoff package and extracting Gpio constructor. The Gpio class configures a GPIO with a certain configuration. In the above example, we have set BCM 4 in the output mode.

You can follow this API documentation of the onoff module to understand various configurations options and API methods.

An instance of Gpio class provides high-level API to interact with that pin. The writeSync method writes either 1 or 0 to the pin which enables or disables it. When a pin is set to 1, it turns on and outputs the 3.3V power. When it is set to 0, it turns off and does not provide any power (0V).

Using setInterval , we are running an endless loop that writes either 0 or 1 to the ledOut pin using ledOut.writeSync(val) method call. Let’s run this program using Node.js.

$ node rpi-led-out.js

Since this is an endless loop, once we start the program, it will not terminate unless we interrupt it forcefull using ctrl + c . During the lifetime of this program, it will toggle the BCM 4 pin every 3 seconds.

One interesting thing about the Raspberry Pi GPIO, once a GPIO pin is set to 1 or 0, it will stay like that until we override the value again or turn off the power supply to the Raspberry Pi. When when you start the program, the LED is off but when you stop it, the LED might remain on.

Input example with Switch

As we know, when a GPIO is used as an input, we need to supply a voltage close to 3.3V. We can hook up a switch (push button) that supplies a voltage directly from 3.3V pin as shown in the circuit diagram below.

(Simple Button Input)

We have used a 1K ohm resistor before the input of the switch to provide some resistance in the circuit. This will prevent too much current drawn from the 3.3V supply and prevent our switch from getting fried.

We have also attached a 10K ohm resistor that also draws the current from the output of the button and drains to the ground. These types of resistors (because of their position in the circuit) are called pull-down resistors since they drain the current (or atmospheric charge build-up) to the ground.

We can alternatively add a pull-up register which pulls the current from 3.3V pin and provides to the input GPIO pin. In this configuration, the input pin always reads high or 1. When the button is pressed, the switch creates a short circuit between the resistor and the ground draining all the current to the ground and no current is passed through the switch to the input pin and it reads 0. Here is a great video demonstrating the pull-up and pull-down resistors.

The output of the switch is connected to the BCM 17 pin. When the button (switch) is pressed, the current will flow through the switch into the BCM 17 pin. However, since the 10K ohm resistor provides a greater obstacle to the current flow, most current flow through the loop represented by the red dotted line.

When the button is not pressed, the loop represented by the red dotted line is closed and no current will flow through it. However, the loop represented by the grey dotted line is closed, and the BCM 17 pin is grounded (0V).

The main reason to add a 10k ohm resistor is to connect BCM 17 pin to the ground so that it can not read any atmospheric disturbance as input high. By not connecting a input pin to the ground, we keep the input pin in floating state. In that state the input pin can read either 0 or 1 due to atmospheric disturbances.

Now that our circuit is ready, let’s write a program to read input value.

In the above program, we have set BCM 17 pin in the input mode. The third argument of the Gpio constructor configures when we want to get notified of the pin input voltage change. This is labeled as the edge argument since we are reading the value at the edge of voltage rise and drop cycle.

The edge argument can have the following values.

When the rising value is used, we will get notified when the input voltage to a GPIO pin is rising from 0V (to 3.3V). At this position, the pin will read logical high or 1 because it is getting positive voltage.

When the falling value is used, we will get notified when the input voltage is falling to 0V (from 3.3V). At this position, the pin will read logical low or 0 because it is losing voltage.

When both value is used, we will get notified of the above two events. When the voltage is rising from 0V (input high or 1) or falling from 3.3V (input low or 0), we can listen to these events at once.

The none value is not discussed here, read the documentation to know more.

The watch method on a GPIO pin in the input mode watches for the above events. This is an asynchronous method, hence we need to pass a callback function which receives the input high (1) or input low (0) value.

Since we are using both value, the watch method will execute the callback when the input voltage is rising as well as when the input voltage is falling. Based on the button press, you should get the below values in the console.

Pin value 1 (when button is pressed)

Pin value 0 (when button is released)

Pin value 1 (when button is pressed)

Pin value 1 (repeat value)

Pin value 0 (when button is pressed)

If you inspect the above output carefully, we sometimes get duplicate values when the button is pressed or released. Since the physical connection between two connectors of the switch mechanism is not always smooth, it can connect and disconnect many times when a switch is not pressed carefully.

To avoid this, we can add capacity in the switch circuit which charges before the actually current flows in the GPIO pin and discharges smoothly when the button is released. You should give this a try since this is fairly simple.

Combined I/O example

Now that we have a good understanding of how GPIO pin works and how we can configure them, let’s combine our last two examples. The bigger picture is to turn on the LED when the button is pressed and turn it off when the button is released. Let’s first look at the circuit diagram.

(Simple I/O Example)

As you can see from the above example, we haven’t changed a thing from the above two examples. Also, both LED and Switch circuits are independent. Which means our earlier program should work just fine with this circuit.

In the above program, we have GPIO pins configured in the input and output mode individually. Since the value provided by the watch method on an input pin is 0 or 1, we can use the same value to write to an output pin.

Since we are watching switchIn input pin in both mode, the watch will get triggered when the button is pressed sending the value 1 and also when the button is released sending the value 0.