Flowing LED on RaspberryPi using Golang
Flowing LED
raspberry pi
LED
Golang
In this article will make a flowing LED strip flowing in both directions using Golang. If you need more info on LEDs and how can be connected please follow Blinking LED on Raspberry Pi using Golang tutorial. Also if you want to see the same flowing LED circuit in other programming languages, please check the references section.
Components
- 1 * Raspberry Pi (I am using Raspberry Pi 3 Model B)
- 5 * 220Ω Resistor
- 5 * LED
- 1 * Breadboard
- 6 * Jumper wires
- software: go-rpio
Note: you can easily find all components on Amazon or Ebay (in my case, I bought a "Raspberry Pi starter kit" containing a lot of components).
Schematics
To do the schematics I used Fritzing software. For this tutorial, I will connect the cathode to the Raspberry Pi GPIO, also it is possible to connect the anode to the Raspberry Pi GPIO, check Blinking LED on Raspberry Pi using Golang tutorial for details.
Here is the schematic connecting all 5 LEDs using the cathode of the LED connected to the Raspberry Pi GPIO. Using the Pinout website for reference, we can check the numbering, starting from physical pin 11(BCM pin 17, Wiring Pi pin 0).
And the physical visualization of the circuit.
Hardware
Now it's time to connect everything according to the diagram presented in the section above.
Software
Note: all the code will be written directly on Raspberry PI console using vim editor. I've chosen this method because the example is simple and doesn't require a long time to edit. A better approach is to edit the source code locally, on your PC, using your preferable editor and then upload the code to Raspberry PI.
Login to Raspberry PI using ssh command from Linux and MacOS or using putty from windows, then open the vim editor using the following command:
$ sudo vim flowingLed.go
Copy/paste the following code.
package main
import (
"fmt"
"github.com/stianeikeland/go-rpio"
"os"
"time"
)
var pinsIndex = []int{17, 18, 27, 22, 23}
var pins [5]rpio.Pin
func main() {
/** Open and map memory to access gpio, check for errors */
if err := rpio.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
/** Unmap gpio memory when done */
defer rpio.Close()
for i := 0; i < 5; i++ {
pins[i] = rpio.Pin(pinsIndex[i])
pins[i].Output()
}
/** Toggle - infinite loop */
for {
/** flowing LED light in on direction */
for i := 0; i < 5; i++ {
pins[i].Low()
time.Sleep(time.Second)
pins[i].High()
}
/** flowing LED light in reverse */
for i := 4; i >= 0; i-- {
pins[i].Low()
time.Sleep(time.Second)
pins[i].High()
}
}
}
Save and close vim.
Note: The numbering from go-rpio library is using BCM numbering, physical pin 11 is BCM 17.
Test
First, you need to copy locally the dependency:
$ sudo go get github.com/stianeikeland/go-rpio
To compile the code run the following command:
$ sudo go build flowingLed.go
To run the application execute the following command:
$ sudo ./flowingLed
You should see the LEDs light flowing from one side to the other and back.
References
- Fritzing software used in this article to generate the schematic and all images.
- Pinout interactive website.
- Flowing LED on RaspberryPi using Python
- Flowing LED on RaspberryPi using C
- Flowing LED on RaspberryPi using Java