RaspberryIO - Hello World!

By Carlos Solorzano - 20 Sep 2019
Raspberry RaspberryIO GPIO IoT API .NET NET.Core

GPIO

GPIO (general-purpose input/output) pins are a powerful feature of the Raspberry Pi that allows it to communicate with the external world.

Through GPIO pins you can interact with physical devices like LEDs, buttons, many kinds of sensors (temperature, humidity, accelerometer, GPS, etc.) or even communicate with other microcontrollers using different communication protocols like I2C or SPI.

RaspberryIO provides access to the GPIOs through low-level libraries that interact directly with the Raspberry Pi hardware. We have been working in implementing two mainly libraries:

  • WiringPi: Fully implemented library, you can use all the WiringPi functionality through WiringPi-dotnet implementation.
  • PiGpio: Partially implemented library. We are working in this implementation, nevertheless, you can already use the most characteristics of PiGpio through PiGpio-dotnet.

Hello World!

On this second post, we are going to extend the console project we did in the first post, using the GPIO pins for blinking an LED.

Material

Finally, We are going to get our hands dirty working with some basic electronic components. For this project we are going to need the following bill of material:

  • 1 x LED of any color.
  • 1 x 100Ω resistor.
  • 2 x Jumper wires (We recommend Male-Female dupont jumper wires, for easiness).
  • 1 x Breadboard.

Schematics

You must set up the next schematic (formal representation of the circuit), in order to get the project working as expected.

Schematics

We are using fritzing app which is an open-source initiative that makes electronics accessible for anyone.

You must connect the LED’s anode to the BCM 17 pin and the LED’s cathode to ground through the resistor. For more information about Raspberry Pi GPIO pinout, follow this comprehensive guide.

Here is an image of the breadboard view, a useful fritzing tool where you can view the electronic parts virtually. This tool is especially useful for those who begin with electronics.

Breadboard

The code

  • Open Visual Studio and open the console project we we did in the first post.
  • In order to get the GPIO pins working, we need to add a library implementation. For this project, we are going to be using the WiringPi implementation since it is the library we are tested the most.

    Add the Unosquare.WiringPi nuget package reference to your project.

      PM> Install-Package Unosquare.WiringPi 
    
  • Add the next function to the Program class:

      static async Task Blink(BcmPin pinNumber, CancellationToken cancellationToken)  
      {  
          var pin = Pi.Gpio[pinNumber];  
          pin.PinMode = GpioPinDriveMode.Output;  
      
          while (!cancellationToken.IsCancellationRequested)  
          {  
              pin.Write(!pin.Value);  
              await Task.Delay(500);  
          }  
      
          pin.Write(false);  
      }  
    

    This function expects a BcmPin enumeration value, that represents the GPIO pin we will be working with and a CancellationToken to finish the task.

  • Modify the Main function to call the Blink function:

      static void Main(string[] args)  
      {  
          Pi.Init<BootstrapWiringPi>();  
      
          Console.WriteLine(Pi.Info);  
          // Blink led  
          var tokenSource = new CancellationTokenSource();  
      
          var task = Blink(BcmPin.Gpio17, tokenSource.Token);  
      
          Console.ReadLine();  
          tokenSource.Cancel();  
          task.Wait();  
    }  
    

    We need to tell RaspberryIO which underlying library implementation it’s going to be using through a bootstrapping process.

      Pi.Init<BootstrapWiringPi>()  
    

    This process loads all the specific classes needed to work with GPIOs and other hardware-dependent features implemented by the underlying library.

Deploy and run the app

For deploy and run the app, follow the instructions from the first post.

Finally, we have our hardware “Hello World!”, blinking an LED.

Hello World!

That’s all for this example. This was the first contact with Raspberry Pi hardware, generating a simple output. In future blog entries, we are going to see more examples using GPIO for simple inputs.

Comments