Single LED circuit with the Pi

In the previous post we configure our Raspberry Pi (and Windows PC) to use C# code to control the GPIO ports of the Pi. With all the settings done, it is time to test the configuration with a simple LED circuit.

So let us start with the list of components we need for this project.

  • The raspberry Pi, (of course, after all the idea is to connect the Pi with our circuit.)
  • Breadboard
  • Resistor. (I will show you bellow how to calculate the values)
  • LED
  • Switch. (I am going to use a push button, but it will work with other type of switches as well).
  • Cables, to connect the Pi to the circuit and the components of the circuit.

RPi3_001

Before we start I will recommend to make a directory for placing all files used for the project, in my case I created two directories a Projects directory and a PiLED directory: /home/pi/Documents/Projects/PiLEDCircuit (you can use the mkdir command to make a new directory). If you are not use to Linux, you can find a list of common Linux commands here.

RPi3_012

Ok, to start let’s take a look at our first circuit diagram.
RPi3_002

As you can see it only consist of a LED (D) and a Resistor (R). The two ends of the circuit are connected to the Raspberry Pi, one end to a 3.3V supply (you can used physical pin #1 or #17) and the other end to physical pin #29 (GPIO 5). The figure below was the best I could find on the internet, because it shows the physical device with the pin physical number and pin name.
RPi3_003

Circuit Basics

Before we start making the circuit, let’s review some basics. (If you already understand the basics feel free to skip this chapter as it will only have a very basic explanation of the circuit and the components)

As you might know a LED is a Light-Emitting Diode. Diodes only allow current to flow in one direction. Diodes re always polarized, so they has two terminals, a positive side called the Anode, and the negative side called Cathode. The Current can only flow from Anode to Cathode when the Anode side is more positive (Voltage is higher) than the Cathode side. Therefore the importance for a diode to be connected in the correct direction.

Every physical diode have some sort of indication to identify the anode or cathode pin. Usually in the LED the longer leg is the Anode (positive side), the short leg is the Cathode. Also, the flat side of the LED’s case (the red plastic part) is the Cathode. (If you want to know, the normal diode will have a line near the cathode pin).
RPi3_004

Resistor is an electronic component which basically do just that “resist” the flow of current across the resistor.

The resistance to current flow results in a voltage drop across the resistor device. In a direct-current (DC) circuit (like the one we are going to make), the current (I) through a resistor is inversely proportional to its resistance (R), and directly proportional to the voltage (V) across it. This is the well-known Ohm’s Law (V = I x R).

To better understand this, imagine the resistor is your bathroom water tap and the water flow is the current. The more you open the tap (less resistance) more water flows out the tap, if you turn the tap lets said halfway (increasing the resistance) less water will flow out the tap. So just keep in mind that the higher the resistance the lower the current (I = V/R).

(Note In alternating-current (AC) circuits, this rule also applies as long as the resistor does not contain inductance or capacitance).

The value of a resistor is expressed in Ohms (Ω). You can figure out the resistance of a resistor from the pattern of colored bands. On most resistors, you will see there are three color bands, and after a short space a fourth band usually colored gold, silver, brown or red.

To calculate the value of the resistor (for a four bands resistor) the first bad represents the first digit, the second band is the second digit the third band is the multiplier and the fourth band is the tolerance. The value of each color, multipliers and tolerances are shown in the figure below.
RPi3_005

For example, a resistor in the figure with the colors (Green, Blue, Yellow, Silver) will be: First digit Green = 5, second digit Blue = 6, therefore we have 56, the multiplier is Yellow = 10K so we have 56 x 10K = 560KΩ with a tolerance Silver = 10%.

Finally let’s end the circuit basics with the explanation of the breadboard. The breadboard is a “board” for testing and prototyping circuits, no soldering is required, which makes it faster and easier to test your circuits.

To understand how the breadboard works see the following figure.
RPi3_006

In the figure the green lines over the holes shows how the board is internally connected, meaning that everything you place along the same green line will be connected. The side (vertical) rows are used for the power supply usually identify with a Red line on the side and Ground identify with a blue line. You will also notice that the side (vertical) rows can be all connected or divided in two (but you usually will have a way to identify which case it is: such as the continuity of the side colored lines, or if there is no line the space between hole’s set will be bigger at the separation).

The circuit

To start with our circuit lets first calculate the value of the resistance we need to use for our circuit. From the data sheet of your LED you can find the Voltage drop, (usually about 1.7V for a standard red LED, 2.2V for a super bright red and green LEDs, and around 3.2V for blue and white LEDs). The max current for most standard LEDs is about 20mA.

Our supply Voltage will be 3.3V therefore using the Ohms law we can calculi the resistance we need

R = V/I = (3.3V-1.7V)/0.02A = 80Ω

Any resistor with a value over 80Ω will be fine.

Note from the GPIO diagram above, that the 3.3V supply pin and the 5V supply pin are next to each other, so is better to calculate a resistance for the 5V supply as well:

R = V/I = (5V-1.7V)/0.02A = 165Ω

I would select a value over 165 Ω for my circuit, so that if I misplace the supply and use 5V instead of 3.3 the circuit will still works fine. For my circuit I am using a Resistor of 330Ω. Now with the value of the resistance we can finally make our circuit.
RPi3_007

From the figure notice that one end of the resistor goes to the supply (connected to the Pi by the Red cable), the other end is connected to the LED’s Anode. The LED’s cathode is connected to the Pi’s GPIO by the orange cable.

The Application Program

Let’s write the program to test our LED circuit. In Visual Studio create a new C# Console Application and name it (in my case I named it PiLEDCircuit). Once the project is created, on the Solution Explorer right-click on the References node and select Add Reference…
RPi3_008

The Reference Manager windows will open. In there, browse to WiringPi.dll file we created in the previous post, selected and click Ok.
RPi3_009

After you Add the WiringPi.dll file you can confirm that it was added to the References and make sure to added to your program (using WiringPi).
RPi3_010

Now let’s code. The program will be simple, the steps to follow are:

  1. Initialize the GPIO
  2. Tell the Pi we will send data out the GPIO
  3. Make sure LED is initially OFF
  4. Ask the user to turn the LED ON by pressing a key
  5. Turn LED ON
  6. Ask the user to press a key to Turn LED OFF and exit

The code is here:

using System;
using WiringPi;

namespace PiLEDCircuit
{
    class Program
    {
        //Place the LED on GPIO 5 (Physical Pin 29)
        const int redLedPin = 29;

        static void Main(string[] args)
        {
            // Tell the user that we are attempting to start the GPIO
            Console.WriteLine("Initializing GPIO Interface");

            // The WiringPiSetup method is static and returns either true or false
            // Any value less than 0 represents a failure
            if (Init.WiringPiSetupPhys() >= 0)
            //ensures that it initializes the GPIO interface and reports ready to work. We will use Physical Pin Numbers
            {
                // Tell the Pi that we will send data out the GPIO
                GPIO.pinMode(redLedPin, (int)GPIO.GPIOpinmode.Output);

                //Ensure that the LED is OFF
                //Remember the supply is 3.3V(high) therefore: High-High=0 --> LED is OFF
                GPIO.digitalWrite(redLedPin, (int)GPIO.GPIOpinvalue.High);

                // Tell the user that GPIO Initialization Completed successfully
                Console.WriteLine("GPIO Initialization Complete");

                //Tell the user to press any key to Turn ON the LED
                Console.WriteLine("Press any Key to Turn LED ON");
                // Pause and wait for user to press a key
                Console.ReadKey();
                // Turn the LED ON
                GPIO.digitalWrite(redLedPin, (int)GPIO.GPIOpinvalue.Low);
                //Tell the user taht LED should be ON
                Console.WriteLine("Led is ON");

                //Tell the user to press any key to Turn OFF the LED
                Console.WriteLine("Press any Key to Turn LED OFF and Exit");
                // Pause and wait for user to press a key
                Console.ReadKey();
                //Turn LED Off
                GPIO.digitalWrite(redLedPin, (int)GPIO.GPIOpinvalue.High);
            }
            else
            {
                //Tell the user that GPIO Interface did not initialize
                Console.WriteLine("GPIO Initialization Failed!");
            }
        }
    }
}

Once you finish writing your code compile it using the Build option.
RPi3_011

Finally we are ready to deploy it. First we need to connect our windows PC to the Pi and transfer to the Raspberry Pi the PiLEDCircuit.exe and WiringPi.dll files that were created when we compiled our code. (I use PuTTY to connect to the Pi, and command pscp source destination to transfer files).

From the Pi Terminal (or your remote connection) navigate to the folder to which you transferred the files. Once there we can run our application with the following command: sudo mono PiLEDCircuit.exe.
RPi3_013

You will see the message “press any key to Turn the LED ON”. Press any key and:
RPi3_014

Yes! The LED is ON. The Terminal will also show you the message “The LED is ON” as well as “Press any Key to Turn the LED OFF and Exit”.
RPi3_015

After you Press any Key the Application will stop running.
RPi3_016

In this Post, we confirmed that we could control the GPIO ports with C#, however we only wrote data out the GPIO, in the following post we will use a Push Button to tell the Pi to Turn ON/OFF the same LED.

See you!

Advertisements

2 thoughts on “Single LED circuit with the Pi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s