The RGB LED will be controlled by software PWM. When different values are set to the legs, they make different colors.

Hardware

Component List

  • Raspberry Pi 3 B+ x 1
  • GPIO Extension Board and GPIO Cable x 1 (If you do not have an Extension Board, connect Raspberry Pi and breadboard directly with jumper wires.)
  • Breadboard x 1
  • RGB LED x 1 (This is a common anode RGB LED. If you have a common cathode RGB LED, the circuit should be different.)
  • Resistor 220Ω x 3 (Check your RGB LED specification for the correct resistor to use.)
  • Jumper Wire x 4

Schematic Diagram

Breadboard Diagram

Note that the longest leg of the LED needs to be connected to the positive. It is the second to the left as shown in the diagram. The sequence of the legs, from left to right is R, (+), G, and B.

Program

Create a Console Project.

Add NuGet package Unosquare.Raspberry.IO.

Main Program: Program.cs

using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;

... 

GpioPin pinR = Pi.Gpio.Pin25;    // pin 37, GPIO26
GpioPin pinG = Pi.Gpio.Pin24;    // pin 35, GPIO19
GpioPin pinB = Pi.Gpio.Pin23;    // pin 33, GPIO13

if (pinR.IsInSoftPwmMode == false)
{
    pinR.StartSoftPwm(100, 100);
}

if (pinG.IsInSoftPwmMode == false)
{
    pinG.StartSoftPwm(100, 100);
}

if (pinB.IsInSoftPwmMode == false)
{
    pinB.StartSoftPwm(100, 100);
}

// white
pinR.SoftPwmValue = 0;
pinG.SoftPwmValue = 0;
pinB.SoftPwmValue = 0;
System.Threading.Thread.Sleep(2000);

// red
pinR.SoftPwmValue = 0;
pinG.SoftPwmValue = 100;
pinB.SoftPwmValue = 100;
System.Threading.Thread.Sleep(2000);

// green
pinR.SoftPwmValue = 100;
pinG.SoftPwmValue = 0;
pinB.SoftPwmValue = 100;
System.Threading.Thread.Sleep(2000);

// blue
pinR.SoftPwmValue = 100;
pinG.SoftPwmValue = 100;
pinB.SoftPwmValue = 0;
System.Threading.Thread.Sleep(2000);

// yellow
pinR.SoftPwmValue = 0;
pinG.SoftPwmValue = 0;
pinB.SoftPwmValue = 100;
System.Threading.Thread.Sleep(2000);

int steps = 5;

// varying colors
for (int r = 0; r <= steps; r++)
{
    pinR.SoftPwmValue = r * 100 / steps;

    for (int g = 0; g <= steps; g++)
    {
        pinG.SoftPwmValue = g * 100 / steps;

        for (int b = 0; b <= steps; b++)
        {
            pinB.SoftPwmValue = b * 100 / steps;

            System.Threading.Thread.Sleep(150);
        }
    }
}

Unosquare.Raspberry.IO uses the pin number the same as Wiring Pi. At Terminal, enter command gpio readall to see pin number mapping. Use the number in column wPi to address the pins.

There are different types of LED Bar Graph. The one shown here is a 10 Segment LED Bar Graph.

Hardware

Component List

  • Raspberry Pi 3 B+ x 1
  • GPIO Extension Board and GPIO Cable x 1 (If you do not have an Extension Board, connect Raspberry Pi and breadboard directly with jumper wires.)
  • Breadboard x 1
  • 10 Segment LED Bar Graph x 1
  • Resistor 220Ω x 10 (Check your LED Bar Graph specification for the correct resistor to use.)
  • Jumper Wire x 11

Schematic Diagram

Breadboard Diagram

Note that on the diagram, pin 1 of the LED Bar Graph is on the lower-left corner.

In this circuit, the power for the LED is from 3.3V pin. When the GPIO is low, the light will be on. When the GPIO is high, the light will turn off.

Program

Create a Console Project.

Add NuGet package Unosquare.Raspberry.IO.

Main Program: Program.cs

The program will turn on/off the lights on the LED Bar Graph one by one.

using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;

...

GpioPin[] pins = new GpioPin[]{
    Pi.Gpio.Pin29,  // pin 40, GPIO21
    Pi.Gpio.Pin28,  // pin 38, GPIO20
    Pi.Gpio.Pin27,  // pin 36, GPIO16
    Pi.Gpio.Pin26,  // pin 32, GPIO12
    Pi.Gpio.Pin11,  // pin 26, CS1
    Pi.Gpio.Pin10,  // pin 24, CS0
    Pi.Gpio.Pin06,  // pin 22, GPIO25
    Pi.Gpio.Pin05,  // pin 18, GPIO24
    Pi.Gpio.Pin04,  // pin 16, GPIO23
    Pi.Gpio.Pin01}; // pin 12, GPIO18

// turn on one by one
for (int pin = 0; pin < pins.Length; pin++)
{
    pins[pin].PinMode = GpioPinDriveMode.Output;
    pins[pin].Write(false);
    System.Threading.Thread.Sleep(500);
}

System.Threading.Thread.Sleep(1500);

// turn off one by one
for (int pin = 0; pin < pins.Length; pin++)
{
    pins[pin].Write(true);
    System.Threading.Thread.Sleep(500);
}

Unosquare.Raspberry.IO uses the pin number the same as Wiring Pi. At Terminal, enter command gpio readall to see pin number mapping. Use the number in column wPi to address the pins.

In this project, the LED light is driven by PWM (Pulse Width Modulation). The LED will be brightened step by step.

There are two ways to use PWM, hardware and software. The hardware PWM can only be used on the GPIO that supports PWM. The software PWM can be used on almost every GPIO pins.

For Raspberry Pi 3B+, GPIO18 (pin 12) supports hardware PWM.

Hardware

Component List

  • Raspberry Pi 3 B+ x 1
  • GPIO Extension Board and GPIO Cable x 1 (If you do not have an Extension Board, connect Raspberry Pi and breadboard directly with jumper wires.)
  • Breadboard x 1
  • LED x 1
  • Resistor 220Ω x 1 (Check your LED specification for the correct resistor to use.)
  • Jumper Wire x 2

Schematic Diagram

Breadboard Diagram

Note that the longer leg of the LED needs to be connected to the positive. It is shown as bended leg in the diagram.

Program 1 Hardware PWM

Create a Console Project.

Add NuGet package Unosquare.Raspberry.IO.

Main Program: Program.cs

using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;

...

GpioPin pin = Pi.Gpio.Pin01;    // pin 12, GPIO18
pin.PinMode = GpioPinDriveMode.PwmOutput;
pin.PwmRange = 100;

// turn brighter each step
for (int i = 0; i <= 5; i++)
{
    pin.PwmRegister = i * 20;
    System.Threading.Thread.Sleep(1000);
}

// turn darker each step
for (int i = 10; i >= 0; i--)
{
    pin.PwmRegister = i * 10;
    System.Threading.Thread.Sleep(500);
}

Program 2 Software PWM

Create a Console Project.

Add NuGet package Unosquare.Raspberry.IO.

Main Program: Program.cs

using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;

...

GpioPin pin = Pi.Gpio.Pin01;    // pin 12, GPIO18

if (pin.IsInSoftPwmMode == false) 
{
    pin.StartSoftPwm(0, 100);
}

// turn brighter each step
for (int i = 0; i <= 5; i++)
{
    pin.SoftPwmValue = i * 20;
    System.Threading.Thread.Sleep(1000);
}

// turn darker each step
for (int i = 10; i >= 0; i--)
{
    pin.SoftPwmValue = i * 10;
    System.Threading.Thread.Sleep(500);
}

Unosquare.Raspberry.IO uses the pin number the same as Wiring Pi. At Terminal, enter command gpio readall to see pin number mapping. Use the number in column wPi to address the pins.

In this project, I will show you how to make a blinking LED. This is like a Hello World project for GPIO.

Hardware

Component List

  • Raspberry Pi 3 B+ x 1
  • GPIO Extension Board and GPIO Cable x 1 (If you do not have an Extension Board, connect Raspberry Pi and breadboard directly with jumper wires.)
  • Breadboard x 1
  • LED x 1
  • Resistor 220Ω x 1 (Check your LED specification for the correct resistor to use.)
  • Jumper Wire x 2

Schematic Diagram

Breadboard Diagram

Note that the longer leg of the LED needs to be connected to the positive. It is shown as bended leg in the diagram.

Program

Create a Console Project.

Add NuGet package Unosquare.Raspberry.IO.

Main Program: Program.cs

using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;

...

GpioPin pin = Pi.Gpio.Pin25;    // pin 37, GPIO26
pin.PinMode = GpioPinDriveMode.Output;
bool onOff = true;

for (int i = 0; i < 10; i++)
{
    pin.Write(onOff);
    System.Threading.Thread.Sleep(500);
    onOff = !onOff;
}

Unosquare.Raspberry.IO uses the pin number the same as Wiring Pi. At Terminal, enter command gpio readall to see pin number mapping. Use the number in column wPi to address the pins.