Monday, January 6, 2014

Connecting an LCD (HD44780) to Raspberry Pi

Having an LCD connected to your Raspberry Pi is impressive and practical. But for a RPi-based device that is working as stand-alone, it is a fundamental element. How else can the user get information or see his/her options?

A cheap and easy to begin with LCD is the HD44780. Actually, the coding is not the LCD itself, but the controller of the display. The display comes both in different sizes:
  • 2 lines by 16 characters
  • 4 lines by 20 characters
  • other, non popular dimensions
Also, you can get them in different back-light colors, blue with white letters, or green with black letters.
the largest version of HD44780 with blue back-light
the popular 2-lined version with green back-light

What is the best choice? Well, for projects that have a menu, such a webradio, the big screen is more convenient. But again it's more expensive ($6-12 USD, 5-8€). The popular 16x2 is cheaper ($3-6USD, 2-5€) and more suitable for status messages (such as IP address, temperature etc.).

When you order it, don't forget to mention the back-light color that fits you best (I prefer the blue one).

So what is the next step after acquiring it? Wiring it of course. As you will see in the top left side of the LCD, there are 16 pins. But don't worry, you won't be needing them all. Here's an overview:


  • Generally the first two and the last two pins are about powering the LCD. The first set (pins 1-2) is about the display of characters and the last set (pins 15-16) is about the back-light. When powering, we mean the +5V pin from Raspberry Pi, not the +3.3V one.
  • Pins 1, 5 and 16 are all connected to ground (-). Pin 5 is ground because we only want to write to the screen (0), not read (1).
  • Pin 3 is about managing the contrast. Here we connect a potentiometer and adjust the contrast (must-do, because the default contrast is always not good enough).
  • Pins 4 (Register Select or RS) and 6 (Enable or EN) are about controlling the LCD (e.g. clearing the screen, setting the cursor etc.)
  • Pins 7-14 are supposed for the characters to be displayed. That's 8 pins if you are going to program it in 8-bit mode, which again means 8 cables to Raspberry Pi, only for the characters! But we can do with less, meaning only with 4 cables (4-bit mode). It's slower, but the saving on pins is big.
What/how many pins we need from the RPi side:
  • Pins 2 and 15 from the LCD connect to RPi pin +5V.
  • Pins 1, 5 and 16 from the LCD go to any ground pin on RPi.
  • Pins 4 (RS), 6 (EN) and the first 4 data pins from the LCD (pins 7-10) connect to any free GPIO pins (not +3V3/+5V and not ground). 
  • Pin 3 (contrast) connects to the potentiometer which in turns connects its other two pins to +5V and ground.
Because it's 8 pins to the Raspberry (+5V, ground, RS, EN and 4 data pins), it's easy to make a mistake. So my advise is to start simple, and that is to only connect what is related to power and the potentiometer. That means, first connect pins 1,2,3,15 and 16 from the LCD side.

Here's how you will connect the LCD with the potentiometer as you look at it from the back side (not viewing the knob):

When you manage to do the above and you power on the Raspberry, you should see something like this:

So you will see the first line of the LCD with block characters. When adjusting the potentiometer, you should be able to adjust the visibility/blurriness of these blocks. That means: so far so good!

The next steps is to wire the remaining pins, which are pins 4 (RS), 6 (EN) and the first 4 data pins from the LCD (pins 7-10). Make sure you write down what pin from the LCD goes to what pin from the Rapsberry!A useful Excel file to note is this.

In the next article about the HD44780 I will go into details about programming it. If you can't wait and want to play with your newly wired LCD, try the Python code from Adafruit here. Don't forget to change the pin numbers with the ones that you used!

Thursday, January 2, 2014

From breadboard to circuit board (PCB): Now what?

We all have played more or less with our new microcontroller, our Raspberry Pi, Arduino (or something else?). And when external electronics components and wiring are needed we use, what else, a breadboard:


Breadboards (or protoboards) are easy and fun, they give you convenience for your electronics projects and no soldering is required. You have positive and negative "rails" or buses/bus strips which gives you easy access to powering your components. And of course, you also get to have jump wires (or jumper wires):
With them you can connect one breadboard contact point with another without soldering. Made a mistake? No problem, you just take the jump wire out and you place it elsewhere within seconds. That simple.

But what really happens when you have built a real nice, useful or practical project that you need to make it "permanent", meaning that you want to keep using it for as long as it lasts? Then the breadboard is no more use, let alone that you will probably want your (one and only) breadboard for your new and upcoming projects.

Well, it's then time to go to a board and do the soldering job, to permanently connect your electronics elements with each other on a circuit board. What does this mean for you? First of all you will need some soldering equipment, such as:
  • A soldering iron
  • A soldering iron stand, to hold your soldering iron when not soldering
  • A desoldering pump, to remove folder from places where there shouldn't be.
  • Desoldering wire (optional)
  • Cleaning sponge (optional - you can use a normal sponge)
  • Solder wire
  • Tweezers to be able to pick and place your components in the right position
You can get the above items in the form of kits from e-shops or eBay with prices starting from 8€:


What I have also found very convenient is the "soldering helping hand" or "third hand soldering iron stand", usually accompanied with a magnifying glass:


This thing has adjustable hands which keep the cables/components close to each other while you are holding the soldering iron and the solder. As cheap as 5€.

OK, now you know what you need to solder, but how do you solder? That needs quite some training. Look for some videos on Youtube, they are really helpful, such as this one:



The next thing you need now is some "circuit boards". Here there are two options basically:

Perfboards: These can get very tricky, as they differ quite from the breadboard you are used to. To start with, there are no "rails" or "buses" or "conductive tracks" to help you have things easily connected. They are like independent pin grids and you will need some special wire to create your own "conductive tracks" (called verowire):


Stripboards or veroboard: Here the grid of holes have "rails" or "buses", but they are full of them. That means, that you are running the risk of short-circuit if you are not careful. Basically, what you need to do here is to divide the strips by making "breaks":




Happy soldering!

Thursday, December 26, 2013

Building a webradio with Raspberry Pi from scratch - Part I

I have always wanted to build a web radio from the moment I first started playing around with Raspberry Pi since early 2012. Of course, back then I was missing important experience and knowledge about electronics, which I now have (at least better), and therefore this "weekend project" was always postponed...

But why would someone want to build a webradio? Here are some reasons:
  1. You want the convenience of changing stations and adjusting the volume just like with a plain old radio (OK, not the REAL old radio, because it didn't have radio presets...). This is especially useful when you are living away from your country, and the local radio plays of course LOCAL stations and not your favorite ones back home.
  2. Building a project like this is complex and requires knowledge and experience in different aspects such as: user interface, events, casing etc. When you manage to finish something sophisticated like this you will have a nice feeling of achieving something hard.
  3. The existing off-the-shelf web radio devices out there can be expensive and don't offer exactly what you may want it terms of functionality, so why compromise?
So, how does one start with a project like this? I think the first thing to do, is to think about and write down what exactly functionality you want from your own webradio. Examples:
  • The device should have the ability to manage (add/remove) radio stations.
  • The user should be able to use it independently, without the use of a computer (web interface, SSH console etc.)

Careful here, this list of requirements can be really endless and you are running the risk here of creating a "monster" project that you will never be able to finish in a reasonable amount of time; meaning that it will in no way be a "weekend project"! Try to prioritize your requirements and start with the really important to you things. After all, its YOUR webradio, no ones else's.

This building down of requirements (or "stories" if you have experience with Scrum software development), will guide you to the interface that you need, physical (button-wise) and non-physical (screen interface). For example, having a requirement that the device should be operating absolutely independently (no web interface, no SSH etc.) means that the user should have a screen (e.g. an LCD display) and some buttons that he/she would be able to push to get the expected functionality.

Let's take a look at my requirements:
  • The device should be completely independent.
  • The user should be able to scroll and select from a list of predefined radio stations.
  • The user should be able to adjust the volume by using a potentiometer. meaning an analog input.
  • When the user is not selecting a radio station, he/she should be seeing a screen about what is currently played.
  • The speakers will be externally powered and the user will be able to connect his/her own speakers. 

But how should the interface look like? Here's the idea of the front view of the case which would contain the webradio:


So, like said before, we need an LCD display, 3 buttons and one potentiometer for the volume control. The three buttons will behave as follows: "up" will move the radio station selector up, "down" will move it down and "select" will be playing back our selected radio station.

Think about your requirements and layout, and till Part II!

Sunday, September 29, 2013

Raspberry Pi Programming & Alternatives to Python: Bash shell, PHP, C#

Rasberry Pi is a cool device, no doubt about it. But it comes with a language many of us don't like or are not willing to use: Python. So, in this post I will show you some other ways to program in Raspberry Pi.

To make things more practical, I will apply this code to some simple but real world example: The reading of the pushing of a switch button.

What do we need for the example?

Ingredients:
  • One breadboard. It's practical with it!
  • A pack of cables: 3 male-male jumpers and 3 female-female jumpers.
  • A miniature switch. This is like the "reset" switch on Arduino, by the way.
  • One 1K resistor.
  • A led. Not really necessary, but it's useful to confirm for testing/debugging that the button is really pressed and that the circuit is closed.
You can find many "Starter Kits" on e-bay that include most of this stuff.

Execution:

  1. Put the switch somewhere in the middle of the breadboard. Let's say you put the two pins of the switch on rows 13 and 15 of the breadboard. Make sure there is a lot of space below the switch, so putting it on column "e" would be a good idea. So, a good place to put the switch is on e13 and e15 on the board.
  2. Put the led with the negative side (short leg) in c15 and the positive side (long leg) in c17.
  3. Put the resistor legs in c13 and c10.
  4. Now connect each male-male cable with a female-female so that you have a long cable that is female-male. It's good to have them connected in similar colors.
  5. Connect the pin "1" from Raspberry (the pin closest to the SD card, it's also marked on the board) to the breadboard pin a17. This is the +3,3V pin on the Raspberry.
  6. Connect the third pin (from the top) next to the column of pins where you connected the +3,3V cable and plug it in pin a10 on the breadboard. This is the ground pin on the Raspberry.
  7. Connect the 4th pin from the top on the Raspberry on the same column where you connected the +3,3V and plug it on a13 on the breadboard. This is the GPIO 4 (Broadcom) on the Raspberry.
If you followed the above instructions, your setup should look like this:





Initial System Setup
First of all, we will need to make some initial setup. We need to tell the operating system that we are going to use (Broadcom-based numbering) pin number 4 to read the pressing of the switch. We do this as follows:


sudo -i
echo "4" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio4/direction


Explanation about the filesystem structure and GPIO can be found here.
A very useful Raspberry Pi pin out layout for both Broadcom and Raspberry numbering in Excel format can be found here.

1. Bash Shell
The good thing about Bash shell is that it comes "preinstalled", it's already in the Linux system, so no additional packages to be installed. Pretty useful when you have limited space on Raspberry (e.g. when with a 2Gb SD card) or when off-line.

We write the following Bash shell script (e.g. with vi editor):

#!/bin/bash
while true;
do
PIN="`cat /sys/class/gpio/gpio4/value`"

if [ "$PIN" = "1" ]; then
                echo Button pressed!
                sleep 1
fi
done

let's say we save it as "read.sh" in the current directory. We then execute:

chmod +x read.sh

to make the file executable. And finally we type:

./read.sh

Whenever we press the switch button we will now see the "Button pressed!" message. To stop program execution press CTRL+C.

2. PHP
The default Raspbian distro does not have PHP installed, so we need to install it:

sudo apt-get install php5-cli

We then create the following file:

<?php
        while(true)
        {
                $pin = file_get_contents('/sys/class/gpio/gpio4/value');
                if ($pin==1)
                {
                        echo "Button pressed!\n";
                        sleep(1);
                }
        }
?>


let's say we save it as "read.php". Then we type:

php read.php

to run it. Similarly as in Bash Shell, we press CTRL+C to stop execution.

3. C#
To run C# under Raspberry Pi we will need the Mono environment. To install it, run:

sudo apt-get install mono-complete

After installation is complete, create the following file:

using System;
using System.IO;
using System.Threading;

class Program
{
        static void Main(string[] args)
        {
                while (true)
                {
                        var pin = File.ReadAllText("/sys/class/gpio/gpio4/value");
                        if (pin.Trim()=="1")
                        {
                                Console.WriteLine("Button Pressed!");
                                Thread.Sleep(1000);
                        }
                }
        }
}

let's say we save it as "read.cs". Then we need to compile it:

mcs read.cs

and finally we need to make it executable:

chmod +x read.exe

we run it as follows:

./read.exe

As, always, with CTRL+C we stop execution.

By the way, an interesting Raspberry Pi .NET library can be found here.

Happy coding!

Tuesday, September 17, 2013

Keeping People (happy) in the Company

This video presentation by Zach Holman of GitHub is about keeping people in your company by keeping them happy. Not interesting only for bosses but also for developers, have a look!


Saturday, September 14, 2013

Google gets involved in Raspberry Pi!

Google released a new image for Raspberry Pi! Here's the video:

Pros and cons of working in a start-up

An interesting info-graph about the pros and cons of working in a startup (Click to enlarge). Source: Venture Village