IoT-Smart Light Bulbs Controller in Raspberry Pi using .NET Core

IoT Light Bulbs Controller Raspberry Pi using NET CoreRaspberry Pi using NET Core

In today’s article, we will learn to create a simple application for controlling smart light Bulbs in Raspberry Pi using .NET Core application.

In our previous articles Create First .NET Core application in Raspberry Pi we learned the basics of how to create a ‘Hello World’ application. We also learned how to  Set up Raspberry Pi – Step by step and also Configure .NET Core IoT App on Raspberry Pi.

Today in this article, we will cover below aspects,

Prerequisites:

The smart light Bulb controller application

  •  LED, BreadBoard with 3 male to female jumper wires and 220 Ohm resistor
  • We need an LED, BreadBoard, 3 male to female jumper wires and 220 Ohm resistor for this exercise.
  • GpioController class from “System.Device.Gpio” provides control on accessing the General Purpose Input/Output(GPIO) ports.
  • We have used Pin 7 from the breadboard GPIO port for controlling LED bulbs.

Let’s create a console application using the below command in a Raspberry Pi Terminal window.

dotnet new console

Above command shall add .csproj file and Program.cs file. 

Add the below code to the Main() method in Program.cs as below

using System;
using System.Device.Gpio;
using System.Threading;

namespace led_blink
{
    class Program
    {
        static void Main(string[] args)
        {
            var pin = 7;
            var lightTimeInMilliseconds = 500;
            var dimTimeInMilliseconds = 500;
            
            Console.WriteLine($"Let's blink an LED!");
            using (GpioController controller = new 
            GpioController(PinNumberingScheme.Board))
            {
                controller.OpenPin(pin, PinMode.Output);
                Console.WriteLine($"GPIO pin enabled for use: {pin}");

                Console.CancelKeyPress += (object sender, 
                ConsoleCancelEventArgs eventArgs) =>
                {
                    controller.Dispose();
                };

                while (true)
                {
                    Console.WriteLine($"Light for 
                    {lightTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.High);
                    Thread.Sleep(lightTimeInMilliseconds);
                    Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
                    controller.Write(pin, PinValue.Low);
                    Thread.Sleep(dimTimeInMilliseconds);
                }
            }
        }
    }
}

The project file should use the below package( you can try using the latest available version of “System.Device.Gpio“).

 <ItemGroup>
    
    <PackageReference Include="System.Device.Gpio" Version="0.1.0- prerelease.19376.1" />

  </ItemGroup>

Note: Please use the latest available version of GPIO

Before running the app please make sure to connect LED with the right polarity to avoid any issue.

After a successful setup, the LED light bulb will start blinking as shown below,

blank

Happy Coding !!

Please sound off your comments below if any.

Useful references :



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “IoT- Light Bulbs Controller Raspberry Pi using .NET Core

  1. Thanks. Very helpful. I do see some limitations on GPIO usage as they are currently providing limited API and it’s SDK still evolving.

    1. Thank you Binnet for your comments.You are right. GPIO and other IoT SDK/Libraries are still evolving. Have you seen recent feature added to .NET Core 3.0 ? I shall give a try and soon will post on the updates from that. Once gain thanks for your time.

Leave a Reply

Your email address will not be published. Required fields are marked *