Raspberry Pi - Blinking an LED with Rust
In this blog post, we will learn how to make an LED blink with Rust on Raspberry Pi.
Prerequisites
- Raspberry Pi (I am using Raspberry Pi 4 Model B)
- LED (8mm, 3.5V, 30mA)
- Resistor (220Ω)
- Breadboard
- Jumper wires (2 MF)
We assume that you have already installed Rust on your Raspberry Pi. If not, you can follow the instructions here.
GPIO Pins
GPIO stands for General Purpose Input/Output. Raspberry Pi has 40 GPIO pins. These pins are used to connect external devices to the Raspberry Pi. These pins can be used as either input or output pins. We will be using these pins to connect our LED to the Raspberry Pi.
Image Source - Raspberry Pi Documentation |
The numbers in the above image are the Physical Pin Numbers. Also known as the Board Pin Numbers. The numbers in the image below are the BCM Pin Numbers. Also known as the GPIO Pin Numbers. These numbers are used in the code to refer to the GPIO pins.
Image Source - Raspberry Pi Documentation |
The Circuit
The LED that I am using has the following specifications:
- Forward Voltage: 3.5V
- Forward Current: 30mA (0.03A)
One can tap into 3.3V or 5V power provided by the Raspberry Pi pins. Since, the LED requires 3.5V, we will be using the 5V pin. Also, we need a resistor to limit the current flowing through the LED. We can use the following formula to calculate the value of the resistor:
|
|
The value of the resistor in Ohms will be:
|
|
So, we need at least a 50 ohm resistor. I’ll choose 220 ohms. The higher the resistor value you use, the dimmer the LED.
Here is the circuit diagram we will be referring:
Circuit Diagram for LED |
We will be using the BCM Pin 23 (Physical Pin 16) to power the circuit. The power flows through a 220Ω resistor and then through the LED. Note that the longer leg of the LED is connected to the resistor, while the shorter leg of the LED is connected to the ground pin 6 of the Raspberry Pi.
The Code
It’s time to write the code that will make our LED blink. Create a new Rust project named led-blinking
:
|
|
Add the following dependency to the Cargo.toml
file:
|
|
The rppal
(Raspberry Pi Peripheral Access Library) crate provides access to the Raspberry Pi’s GPIO, I2C, PWM, SPI and UART peripherals. You can read more about it here.
Now, update the src/main.rs
file with the following code:
|
|
Let’s understand the code. First, we import the thread
and time
modules from the standard library. Then, we import the Gpio
struct from the rppal::gpio
module. The Gpio
struct provides access to the GPIO pins. Then, we import the DeviceInfo
struct from the rppal::system
module. The DeviceInfo
struct provides information about the Raspberry Pi device in use.
Next, we create a new instance of the Gpio
struct. Then, we get the pin 23 using the get
method. The get
method returns a Result<OutputPin, Error>
. We use the unwrap
method to get the OutputPin
struct. Then, we convert the OutputPin
struct into an output pin using the into_output
method.
Finally, we enter an infinite loop. In each iteration of the loop, we set the pin to high using the set_high
method. Then, we sleep for a second using the sleep
method. Then, we set the pin to low using the set_low
method and sleep for a second again.
Build and Run
Now, let’s build the project with cargo build
:
|
|
Then, let’s run the project with cargo run
:
|
|
You should see the LED blinking. If you want to stop the program, press Ctrl + C
.
Glowing LED |
Conclusion
In this blog post, we learned how to blink an LED with Rust on Raspberry Pi. We also learned how to calculate the value of the resistor required to limit the current flowing through the LED. If you have any questions or suggestions, feel free to drop a comment. A few useful links are given below:
Thanks for reading, see you in the next one!