Use more LEDs

431 words | 2018-3-21

One blue LED was simply amazing. I have a few more LEDs in different colors. Let’s do something like a mini-garland!

Let’s see what I have: I have LEDs Three 5mm LEDs: red, yellow and green. Using the table for voltage drops on LEDs of different colors, we calculate the voltage drop across the limiting resistors:

Color V across the LED V across the resistor
Red \(V_{red}=1.63V\) \(V_{R2}=V_{cc}-V_{red}=3.3-1.63=1.67V\)
Yellow \(V_{yellow}=2.10V\) \(V_{R3}=V_{cc}-V_{yellow}=3.3-2.10=1.20V\)
Green \(V_{green}=1.90V\) \(V_{R4}=V_{cc}-V_{green}=3.3-1.90=1.40V\)

Again, we start from the permissible current through the load resistors equal to 8mA:


We round up (to reduce the current through the resistor, but not to increase it) and get for all three resistors \(220\Omega\).

The total current \(I=4 \cdot 0.008=0.032A\), this is below the permissible 50mA, as usual, we first check the circuit on a small power supply.

Schematic diagram

LEDs: D1 - blue, D2 - green, D3 - yellow, D4 - red. Resistors: R1 - \(100\Omega\), R2, R3, R4 - \(220\Omega\). Schematic diagram

GPIO compliance with colors:

GPIO Color
26 Blue
22 Green
27 Yellow
17 Red




Working board Voltage on the red LED Voltage on the yellow LED Voltage on the green LED

Calculate the currents through the resistors:


That is, the assumption about 8mA turned out to be correct and we can safely connect the scheme to Raspberry Pi:


Let’s make some simple effects:

package io.github.yrabbit.kotlin.led

import jpigpio.JPigpio
import jpigpio.JPigpio.*
import jpigpio.Pigpio
import jpigpio.Utils

fun main(args: Array<String>) {
    // dirty hack: need to point java.library.path to the location of

    println("Kotlin Raspberry Pi gentoo64")
    println("You must see a lights :)")

    // Init lib
    val pigpio = Pigpio()

    // Check drive strength on GPIO
    val strength = pigpio.gpioGetPad(0)
    println("Current drive strength:${strength}mA")

    if (strength != CURRENT) {
        pigpio.gpioSetPad(0, CURRENT)
        val newStrength = pigpio.gpioGetPad(0)
        println("New drive strength:${newStrength}mA")

    // Mode output
    for (led in LEDS) {
        pigpio.gpioSetMode(, PI_OUTPUT)

    // forward and backward
    val mode0 = arrayOf(0, 1, 2, 3, 2, 1)
    val mode1 = arrayOf(0, 3, 1, 2, 1, 2)
    val mode2 = arrayOf(0, 1, 2, 3)

    repeat(3) {
        chainBlink(pigpio, mode0)
    repeat(3) {
        chainBlink(pigpio, mode1)
    repeat(3) {
        chainBlink(pigpio, mode2)


fun chainBlink(pigpio: JPigpio, chain: Array<Int>) {
    for (led in chain) {
        // ON
        pigpio.gpioWrite(LEDS[led].pin, PI_HIGH)
        pigpio.gpioDelay(300 * 1000)
        // OFF
        pigpio.gpioWrite(LEDS[led].pin, PI_LOW)
        pigpio.gpioDelay(100 * 1000)

 * Clean up
fun gpioSwitchToInput(pigpio: JPigpio) {
    UsedGPIO.values().forEach {
        pigpio.gpioSetMode(, PI_INPUT)

enum class UsedGPIO(val pin: Int) {
    BlueLedPin(26),   // GPIO26
    GreenLedPin(22),  // GPIO22
    YellowLedPin(27), // GPIO27
    RedLedPin(17)     // GPIO17
val LEDS = arrayOf(UsedGPIO.BlueLedPin, UsedGPIO.GreenLedPin, UsedGPIO.YellowLedPin, UsedGPIO.RedLedPin)

const val CURRENT = 8 // 8mA


Run the resulting JAR:

Well, isn’t it cool? I’m happy:smile: