You have probably already seen that the Raspberry Pi has two LEDs directly soldered to the board, one red and one green. But did you know that it is possible to control this green LED, and sometimes even the red one?
In this tutorial, we are going to see how we can use the green LED of the Raspberry Pi by flashing it to transmit a message.
The necessary equipment
The interesting point with this green LED is that it is already on board the Raspberry Pi. Therefore, we will not need any additional hardware. So we just need to:
And like (almost) every time, a way to control the Raspberry Pi, for example in SSH.
What are the onboard LEDs of the Raspberry Pi used for?
Raspberry Pi have two LEDs (except Zero models which only have green), one red and one green. These LEDs are used by the Raspberry Pi to tell us information about its operating status.
Thus, the red LED is directly connected to the power supply of the Raspberry Pi. It therefore allows us to know whether or not our Raspberry Pi is receiving current.
The green LED on the other hand provides us with a little more complex information, mainly at the time of startup.
I'm not going to give you a complete transcription of the Raspberry Pi / Humans dictionary, but be aware that depending on how this LED flashes, you can know if the Raspberry Pi is booting correctly or if it encounters an error, and if so the type of this mistake.
Turning on the green LED of the Raspberry Pi is writing to a file!
Once the Pi is turned on, the green LED stays off as long as there is no activity on the SD card.
It is possible to slightly modify this behavior and control the way the LED lights up ourselves, simply by writing to a file!
First, we will have to modify the default behavior of the LED by writing
none in the file
/sys/class/leds/led0/trigger with the command below:
sudo sh -c "echo none > /sys/class/leds/led0/trigger"
Once this command is placed, if you open this file you will see that it does not actually contain none as you might expect, but a line where none is entered to show that this is the selected mode.
The default behavior being overwritten, all we have to do is check the LED ourselves. And for that, nothing could be simpler!
To turn the LED on or off, we just need to write to the file