Controlling the GPIO pins on Raspberry Pi board

The aim of this exercise is to prepare software written in the C# language under Windows 10 Core system for Raspberry Pi 3 board. The software application blinks LEDS connected to the appropriate Raspberry Pi GPIO pin ports in specific order. The hardware module contains 4 red LEDS which can be turned on/off using Windows 10 GPIO SDK functions.

Step 1

Hardware preparation. Mount the LEDs module into 40-pin Raspberry Pi connector.

Figure 1: Electronic schematic of the LEDs module
Figure 2: Electronic board of the LEDs module
Step 2

To control selected GPIO pins in C# language available for UWP platform create the UWP solution as it was described in the Hello World! application. In the MainPage.cs file add the source code:

/********************************************************************************************************************************
 *
 *                                              SAMPLE GPIO CONTROL PROGRAM
 *
 *  Sample LED control program for students laboratory purposes. 
 *  Presents the Raspberry Pi 2/3 IoT ability to control
 *  GPIO output pins blinking LEDs.
 *
 *
 *        Raspberry Pi GPIO Layout - Model B+
 *  1 - 3V3     Power                      2 - 5V Power
 *  3 - GPIO2   SDA1 I2C                   4 - 5V Power
 *  5 - GPIO3   SCL1 I2C                   6 - GND
 *  7 - GPIO4                              8 - GPIO14   UART0_TXD
 *  9 - GND                               10 - GPIO15   UART0_RXD
 * 11 - GPIO17                            12 - GPIO18   PCM_CLK
 * 13 - GPIO27                            14 - GND
 * 15 - GPIO22                            16 - GPIO23
 * 17 - 3V3     Power                     18 - GPIO24
 * 19 - GPIO10  SPI0_MOSI                 20 - GND
 * 21 - GPIO9   SPI0_SCLK                 22 - GPIO25
 * 23 - GPIO11                            24 - GPIO8    SPI0_CE0_N
 * 25 - GND                               26 - GPIO7    SPI0_CE1_N 
 * 27 - ID_SD   I2C ID EEPROM             28 - ID_SC    I2C ID EEPROM
 * 29 - GPIO5                             30 - GND
 * 31 - GPIO6                             32 - GPIO12
 * 33 - GPIO13                            34 - GND
 * 35 - GPIO19                            36 - GPIO16
 * 37 - GPIO26                            38 - GPIO20
 * 39 - GND                               40 - GPIO21
 *
 ********************************************************************************************************************************/
 
using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
 
namespace LEDTest
{
  public sealed partial class MainPage : Page
  {
    // Raspberry Pi GPIO pins used for LEDs coontrol 
    private const int LED_PIN1 = 4;   // GPIO04
    private const int LED_PIN2 = 23;  // GPIO23
    private const int LED_PIN3 = 24;  // GPIO24
    private const int LED_PIN4 = 12;  // GPIO12
 
    private GpioPin pin1;
    private GpioPin pin2;
    private GpioPin pin3;
    private GpioPin pin4;
 
    // Raspberry Pi GPIO Pin variables for blinking effect
    private GpioPinValue[] pinValue1 = new GpioPinValue[8];
    private GpioPinValue[] pinValue2 = new GpioPinValue[8];
    private GpioPinValue[] pinValue3 = new GpioPinValue[8];
    private GpioPinValue[] pinValue4 = new GpioPinValue[8];
 
    private DispatcherTimer timer;
    private SolidColorBrush redBrush  = new SolidColorBrush(Windows.UI.Colors.Red);
    private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
    private int Counter = 0;
 
    public MainPage() {
      InitLEDs();                                           // Initialize LEDs blinking procedure
      InitializeComponent();                                // Initialize XAML Display view
 
      timer = new DispatcherTimer();                        // define the timer
      timer.Interval = TimeSpan.FromMilliseconds(500);      // set callback timer to execute each 500 ms
      timer.Tick += Timer_Tick;                             // register callback procedure
      InitGPIO();                                           // Initialze GPIO pins
 
      if ((pin1 != null) && (pin2 != null) && (pin3 != null) && (pin4 != null)) {
        timer.Start();                                      // start the timer
      }        
    }
    // Initial LEDs state 8 stages
    void InitLEDs()
    {
      pinValue1[0] = GpioPinValue.High;                     // pins value state 0
      pinValue2[0] = GpioPinValue.Low;
      pinValue3[0] = GpioPinValue.Low;
      pinValue4[0] = GpioPinValue.Low;
 
      pinValue1[1] = GpioPinValue.High;                     // pins value state 1
      pinValue2[1] = GpioPinValue.High;
      pinValue3[1] = GpioPinValue.Low;
      pinValue4[1] = GpioPinValue.Low;
 
      pinValue1[2] = GpioPinValue.High;                     // pins value state 2
      pinValue2[2] = GpioPinValue.High;
      pinValue3[2] = GpioPinValue.High;
      pinValue4[2] = GpioPinValue.Low;
 
      pinValue1[3] = GpioPinValue.High;                     // pins value state 3
      pinValue2[3] = GpioPinValue.High;
      pinValue3[3] = GpioPinValue.High;
      pinValue4[3] = GpioPinValue.High;
 
      pinValue1[4] = GpioPinValue.Low;                      // pins value state 4
      pinValue2[4] = GpioPinValue.High;
      pinValue3[4] = GpioPinValue.High;
      pinValue4[4] = GpioPinValue.High;
 
      pinValue1[5] = GpioPinValue.Low;                      // pins value state 5
      pinValue2[5] = GpioPinValue.Low;
      pinValue3[5] = GpioPinValue.High;
      pinValue4[5] = GpioPinValue.High;
 
      pinValue1[6] = GpioPinValue.Low;                      // pins value state 6
      pinValue2[6] = GpioPinValue.Low;
      pinValue3[6] = GpioPinValue.Low;
      pinValue4[6] = GpioPinValue.High;
 
      pinValue1[7] = GpioPinValue.Low;                      // pins value state 7
      pinValue2[7] = GpioPinValue.Low;
      pinValue3[7] = GpioPinValue.Low;
      pinValue4[7] = GpioPinValue.Low;
    }
    // Initialize device GPIO. All LEDs control pins set to the output state
    private void InitGPIO()
    {
      var GPIO = GpioController.GetDefault();
 
      // Show an error if there is no GPIO controller
      if (GPIO == null) {
        pin1 = null;
        pin2 = null;
        pin3 = null;
        pin4 = null;
 
        Time.Text = "There is no GPIO controller on this device.";
        return;
      }
      pin1 = GPIO.OpenPin(LED_PIN1);
      pin2 = GPIO.OpenPin(LED_PIN2);
      pin3 = GPIO.OpenPin(LED_PIN3);
      pin4 = GPIO.OpenPin(LED_PIN4);
 
      pin1.Write(pinValue1[0]);                     // set pin1 state LOW/HIGH
      pin1.SetDriveMode(GpioPinDriveMode.Output);
      pin2.Write(pinValue2[0]);                     // set pin2 state LOW/HIGH
      pin2.SetDriveMode(GpioPinDriveMode.Output);
      pin3.Write(pinValue3[0]);                     // set pin3 state LOW/HIGH
      pin3.SetDriveMode(GpioPinDriveMode.Output);
      pin4.Write(pinValue4[0]);                     // set pin4 state LOW/HIGH
      pin4.SetDriveMode(GpioPinDriveMode.Output);
 
      Time.Text = "GPIO pins initialized correctly.";
    }
    // XAML Timer callback function set to be executed each 500 ms
    private void Timer_Tick(object sender, object e) {
      LED1.Fill = pinValue1[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED1 elipse Red/Gray = High/Low
      pin1.Write(pinValue1[Counter]);                                               // Set pin1 value High/Low
      LED2.Fill = pinValue2[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED2 elipse Red/Gray = High/Low
      pin2.Write(pinValue2[Counter]);                                               // Set pin2 value High/Low
      LED3.Fill = pinValue3[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED3 elipse Red/Gray = High/Low
      pin3.Write(pinValue3[Counter]);                                               // Set pin3 value High/Low
      LED4.Fill = pinValue4[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED4 elipse Red/Gray = High/Low
      pin4.Write(pinValue4[Counter]);                                               // Set pin4 value High/Low
      Counter++;
      if (Counter > 7)
        Counter = 0;
    }
  }
}

The code defines GPIO pins 4, 23, 24 and 12 as output. GPIO pins can be set as input/output. Because the board contains already resistors connected to the powers supply and LEDs (see the LEDs module schematic) the GPIO pins are set to turn off the internal pullup resistors. Line code 143: pin1.SetDriveMode(GpioPinDriveMode.Output);. The program simply loads the state of the LEDs from the Array[8] defined in the InitLEDs void. In the Main void the timer callback function Timer_Tick is registered to change LEDs state each 500ms. Callback function Timer_Tick each 500ms changes the state of the LEDs from GpioPinValue.High to GpioPinValue.Low (line 156). On the display, according to the LEDs state, the 4 circles change colour from red to brush synchronously.

Step 3

In the MainPage.xaml the code looks like:

<Page
    x:Class="LEDTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LEDTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
  <Grid>
    <Grid.Background>
      <ImageBrush x:Name="Theme" ImageSource="Assets/AEI.jpg" Stretch="UniformToFill"/>
    </Grid.Background>
    <TextBlock x:Name="ZMiTAC" HorizontalAlignment="Center" Height="35" Margin="0,5,0,0" TextAlignment="Center" Text="ZMiTAC IoT Laboratory" FontSize="24" VerticalAlignment="Top" Width="360" Foreground="#FFD02BF5" RenderTransformOrigin="0.489,2.571" FontWeight="Bold"/>
    <TextBlock x:Name="Time" TextAlignment="Center" Margin="0,55,0,0" TextWrapping="Wrap" Text="500 msec" VerticalAlignment="Top" Foreground="#FF89F99D" FontWeight="Bold" FontSize="18.667" RenderTransformOrigin="0.5,1.8"/>
    <Ellipse x:Name="LED1" Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="40" Margin="-150,145,0,0" Stroke="Black" VerticalAlignment="Top" Width="40"/>
    <Ellipse x:Name="LED2" Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="40" Margin="-50,145,0,0" Stroke="Black" VerticalAlignment="Top" Width="40" RenderTransformOrigin="1.75,0.714"/>
    <Ellipse x:Name="LED3" Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="40" Margin="50,145,0,0" Stroke="Black" VerticalAlignment="Top" Width="40"/>
    <Ellipse x:Name="LED4" Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="40" Margin="150,145,0,0" Stroke="Black" VerticalAlignment="Top" Width="40"/>
  </Grid>
</Page>
Step 4

Start debugging application and after deploy application package to the board SD card it will be displayed on the monitor.

Figure 3: LEDsApplication on the Raspberry Pi display
en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/raspberrypi_rpi/applciation_example_projects.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0