Raspberry Pi – Button as a digital toggle switch

Ability to directly control the GPIO (general purpose input output) pins is one of the reasons the miniature Raspberry Pi is so famous with hobbyist and educationalists.

In this self learning exercise, I will read the status of the button, determine if it is pressed or not and then based on that, switch the LED on or off.

Very basic right? Let’s follow along.

Components

  1. Raspberri Pi 2 Model B (40 pin) with Raspbian OS
  2. Breadboard
  3. Connectors
  4. LED 5mm
  5. Push Button
  6. Resistors – R1 (330 ohm) & R2 (1K ohm)

Wiring It Up

My setup uses the GPIO 23 (pin #16) and GPIO 24 (pin #18), however you can use any GPIO pins for this purpose. If you are using different pins, make sure to update the pin numbers in the program below.

  • GPIO 23 (pin #16) –  Used as input to read the status of button
  • GPIO 24 (pin #18) – Used as output for switching the LED on and off
  • +3.3V (pin #1) – Connected to power rail on the breadboard
  • GND (pin #9) – Connected to ground rail on the breadboard
  • R1 – Connected between LED and ground
  • R2 – Connected between 3.3V power rail and button
20150820-RPI_Push_Button_Toggle_Switch_bb
Push button programmed as a toggle switch to power on/off a load (LED)

Program

You need a program to tie these hardware together; to read the status of the button and send the signals to light up the LED.

I used ‘C’ with WiringPi libraries. I’m quite new to these myself. If you prefer, you can also do the same with Python. Geany is my favorite editor on the Raspberry Pi, works for both C and Python.

Let me quickly explain the functions of WiringPi library used in the program:

digitalWrite() – used to send an output to the GPIO pin. The first parameter is the pin number. Second parameter determines if the pin should be set as either HIGH or LOW (it can take only 2 states).

digitalRead() – used to read the status of the GPIO pin. The only parameter passed is the pin number. The output is either HIGH or LOW and can be checked as below:

Once this part is understood, whipping up a code to capture the pressed state of the button and lighting up an LED is  a piece of cake. Not joking, it really is!

The next part is how to toggle. This is done by remembering the previous state of the button and when it changes, determine whether to switch the LED on or off.

Since the program involves low level access to gpio pins, it will need ‘sudo’ permissions to run this program.

Summary

Here, we saw how to read input from a button and write output to light up an LED using the GPIO pins of a Raspberry Pi. Once we have these basics right, then the same knowledge can be used to drive other loads like a relay, motor etc.

References

WiringPi – GPIO interface library for the Raspberry Pi

WiringPi GPIO Pins – Pin layout for WiringPi specific and Broadcom specific pin modes

Adafruit – Tons of stuff related with Raspberry Pi and Arduino

 

 

 

 

Installing Windows 10 on Raspberry Pi

Raspberry PiRaspberry_Pi, is a wonderful tiny credit card sized computer created by people who were passionate about education. It is a little device that enables people of all ages to explore computing and to learn how to program in languages like Python and Scratch. Due to its small size and relatively low power consumption, it is also widely used by IOT (Internet of Things) enthusiasts.

The Raspberry Pi landscape has been dominated so far by Linux based operating systems like Raspbian, OpenELEC and the very recent Snappy Ubuntu.

With Microsoft rolling out a version of Windows to this platform, it provides an opportunity for many more people to be engaged with the magic of Raspberry Pi.

Step by Step

  • Download the Windows 10 IOT image for Raspberry Pi from here (around 500 MB).
  • Extract Windows_10_IoT_Core_RPi2.msi, from the ISO image that was downloaded in the previous step. I used the popular 7-zip to extract.
  • Install Windows_10_IoT_Core_RPi2.msi on your computer. I used a Windows 7 computer for this.
  • Run ‘Windows IOT Core Image Helper’ from start menu.

Windows_IOT_Core_Image_Helper

  • Select the correct SD card from the list.
  • Select the .ffu image file which is installed in the folder: ‘C:\Program Files (x86)\Microsoft IoT\FFU\RaspberryPi2’.
  • Click on ‘Flash’ button.
  • The ‘Deployment Image Servicing and Management Tool’ will run which will transfer the ffu image file to the SD card.

dism

  • This process takes some time, so please be patient until the status at the bottom shows as below.

Windows_IOT_Core_Image_Helper_Complete

  • I checked how the partitions are divided on the SD card using the Mini Partition Tool, here’s a screenshot.

Windows10_IOT_Partitions

  •  After it is successfully installed, pop the SD card into the Raspberry Pi and power up. I used the Raspberry Pi 2 Model B, the one with the 1GB RAM on it.
  • Again, the process takes some time, so please be patient.
  • Unfortunately, there is no Wifi support (yet), so I plugged mine over an ethernet cable to the router.
  • As I connected it as headless (without connected to a monitor), I watched my router to find out the IP address after it boots up.
  • The default hostname of the new Windows 10 IOT device is ‘minwinpc’
  • Now connect over SSH (you can also use powershell) using the default credentials (username: Administrator, password: p@ssw0rd)

Windows_IOT_SSH_Login

  • After logging in over SSH a familiar DOS like interface is displayed with the familiar C:\> prompt.
  • Below screenshot shows the default shares setup on the device.

Windows_IOT_Default_Shares

 

Congratulations! Windows 10 IOT has been successfully setup on your Raspberry Pi!

 

References:

  1. https://dev.windows.com/en-us/iot
  2. http://ms-iot.github.io/content/en-US/win10/SetupRPI.htm
  3. https://github.com/ms-iot/samples
  4. https://www.raspberrypi.org/