Lighting LED Using Raspberry Pi and Python

Lighting LED using Raspberry Pi

To lighting LED using Raspberry Pi we first need Python GPIO. In my previous post on Raspberry pi we have installed raspbian. Next thing is to start experimenting on it. In this post we will see how you can setup your pi to run Python and create your first Project.

As a first step we need to make sure that we have Python GPIO library installed and working. To do so follow the following steps.

Installing The Python GPIO Library

Python should already be installed in your Raspberry Pi. Next step would be to install GPIO library.

Installation

If you have the latest version of Raspbian then most likely Rpi.GPIO is pre-installed. You just need to update your library using the following commands.

sudo python
import RPi.GPIO as GPIO
GPIO.VERSION

To update the library use following commands.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install rpi.gpio

Alternate way to install Python GPIO Library:

 

Important: You must be a superuser to run scripts that control the GPIO pins on your RPi. If you are using the IDLE IDE to write your Python scripts, be sure to launch it as a superuser.

Your first Project – lighting up a LED

Parts Required:

  1. Raspberry Pi 2 ( you can use Pi 3)
  2. A small LED( any colour, I have used RED)
  3. 1 100 ohm resistance
  4. Some wires/connectors
  5. Breadboard

At first we will start with lighting the LED with 3.3v pin of Raspberry Pi. For this use the following Schematic:

pi_schematics_1

  • Pin 1 (+3.3 volts) should go to the longer leg of your led. This pin provides a steady supply of 3.3v. Unlike the GPIO pins on your Pi, this pin is not programmable, and cannot be controlled by software.
  • Attach the shorter leg of the led to the resistor. Finally, attach the other end of the resistor to Pin 6 (- GND) on your Pi

If everything goes well, LED should light up when turning on the Raspberry Pi.

Next, we will make this LED to blink by using the GPIO pin 4( or board Pin 7). For this we will write small piece of code in Python.

Launch idle IDE as superuser(root).

sudo idle

type following into the code window and save the file as python1.py

import RPi.GPIO as GPIO 
import time
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM) ## Use BCM pin numbering
GPIO.setup(4, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
while True :
    GPIO.output(4,True)
    time.sleep(5)
    GPIO.output(4,False)
    time.sleep(5)

Make connections as shown below:

lighting LED using Raspberry Pi

To run the code press ctrl+f5. You show now see LED blinking with a delay of 5 sec. That’s it for now. We will see some complex code in next post.

Multi browser testing using Visual Studio

Multi browser testing using Visual Studio can be achieved by using Selenium and NUnit plugins. Below are step by step details of how it can be achieved.

Step 1

In order to achieve multi browser testing using Visual Studio, we need to install few extensions and plugins. First is NUnit adapter ( used in test execution and NUnit Test framework) and Selenium Web Drivers.

Install Nunit adapter for Visual Studio from Tools-> Extensions and updates..

Step1 extensions and updatesStep1 extensions and updates 2

Similarly install NUnit framework extension.

Step 2

Create an empty Test project in Visual Studio as shown below.

Step2 create project

Select the newly created project and click on “NuGet package manager” and search for Selenium. It will list selenium WebDriver for different browsers. Select them and install. In my example I have selected for browser Edge, Chrome, and Firefox.

Step2 select package managerStep2 select selenium webdrivers 2Step2_installedAfter Installation of Web drivers, they will be automatically added to project references.

Alternative way to add references is by manually downloading the respective Web Drivers and adding references in the project.

Step 3

Now we will start on writing code for launching the browsers. Add a new item of type class file and add following code.

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;

namespace Sample1
{
    class Sample1_Edge
    {
        private IWebDriver driver;
        [SetUp]
        public void Setup()
        {
            driver = new EdgeDriver();
        }

        [Test]
        public void SampeBrowserLaunch1()
        {
            driver.Navigate().GoToUrl("http://digitalab.org");
        }
        [TearDown]
        public void TearDown()
        {
            driver.Quit();
            driver.Dispose();
        }
    }
}

Note the 3 important sections. Setup, Test and TearDown. Test will repeat for multiple tests.

For other browsers, you need to change the following code.

driver = new EdgeDriver();

to

driver = new FirefoxDriver();
driver = new ChromeDriver();

Step4

Build the code and run the test using Test Explorer.

Step4 Test execution

 

In the next post we will structure this and use xml file to control multiple browser settings.

In this example of Multi browser testing using Visual Studio, we have used VS 2015. But this will also work with version 2013. It should also work with VS 2012 but I have not tested it.

Arduino Uno

What is Arduino UNO?
Arduino is an open source electronics prototyping platform based on flexible, easy-to-use hardware and software. It is intended for artists, designers, hobbyists and anyone interested in creating interactive objects or environments.
C:\Users\Pintu\OneDrive\Blogs\Electronics\Adrino_UNO_image.png
Arduino Uno is a board based on ATmega328 microcontroller. It has 14 digital input/output pins(of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.
Schematics:
Arduino Uno Circuit
The Uno is open-source hardware! You can build your own board. For more detail on it refer the official site here.
Since it is an open source there are many clones available in the market at a very cheap price.
The microcontroller on the board is programmed using Arduino programming language using Arduino development environment.
The Arduino UNO is programmed using Arduino IDE software. Atmega328 on Arduino UNO comes with a boot loader that allows you to upload new code to it without using an external hardware programmer. It uses STK500 protocol to communicate. You can bypass the boot loader and program the microcontroller through ICSP (in-circuit serial programming) header, but using boot loader programming is quick and easy. Select the correct board from ‘Tools→Board’ menu in Arduino IDE and burn the program (sketch) through standard USB port in the computer.
Refer the following YouTube video for introduction to the Arduino UNO.

Raspberry Pi 2 Pinouts

In my earlier blogs I have shown you how to install the OS and use it. Now we are ready to start building the projects. To do so we first need to understand what each pin does. The diagram below shows raspberry pi 2 pinouts and what each pins can do.

rapberry pi 2 pinout 1

Raspberry Pi 2 pinout figure 1

In order to understand pin number, make sure to have your pi oriented as shown in the figure. Now look at the centre two columns on the chart. These shows the Physical Pin number. The outer two columns of the chart show you the bcm numbering. The numbering system that you use depends on how you configure things in the software. There are 2 numbering sachems. One is BCM(Broadcom)

which is more common and will be using for most of the projects and the other is  WiringPi. The picture below shows WiringPi numbering.

raspberry pi2 pinout 2

Also you can notice that some of the pins are multi-purpose. For example pin3 and 5 can be GPIO pins, or they can be configured for I2C. Similarly, 8 and 10 can be General purpose or can be Tx and Rx. Also note that the GPIO pins are analogous to your digital input/output pins on Arduino.

 

 

Raspberry Pi 2 pinout figure 2