Blogging is fun

Introduction to Hardware Programming (Part 1)

Posted 2015-10-26 By Otto Remse

In this blogpost I'll try to backtrack on the previous code night on "Introduction to hardware programming" and go through the examples and principles that we touched on and explain them in a lecture-oriented manner.
I'll also go abit further than the code night and give examples in python and on subjects that were not included in the code night. 
After writing this for a while I noticed that there would be a _lot_ of material, so I'll chop this up in easily consumed chunks :)

For most of the examples we will be using Autodesks free circuit-simulator 123dcircuits, which actually has arduino-boards that you can program as well! A great platform and way to get started on arduino-programming without having to invest on any hardware! Go sign up here: (​https://123d.circuits.io/​

As with most things, we will start with the basics: 

electricity and circuits!

A direct current circuit has a positive and negative(ground) end and a load between them.
Electricity will always choose the shortest or "easiest" path to complete the circuit. 
A circuit where the easiest path contains no load is called a short-circuit and you don't want one of those :)

Conventional and electron flow notations

One of the major misconceptions about electricity is how it flows. In circuit schematics the flow will always be from positive to negative(conventional flow notation).

 
But in reality it flows from negative to positive (electron flow notation)


This was never changed after the discovery due to the widespread usage of the conventional flow notation. And also the association of positive to an "abundance"" and negative with a "deficite".
A real life circuit is filled with negativly charged electrons flowing from negative to positive poles.
Some of you sciency-people might object that there are also positivly charged protons in the circuit, and while this is true and while they are of equal charge of the electrons, they are stationary in solid metals, so we don't need to account for them.
Albeit if you were, stupidly enough, about to electrocute yourself, the charge would not be made out of electrons, but instead of negative and positive-charged atoms (ions) I.E positive sodium and potassium atoms, negative chlorine and so on.

Your first circuit! 

A really basic circuit that still holds educational value is that of a glowing LED, like the one we have here: Single-LED Circuit (click start simulation to see it work).

The first thing we need to elaborate is that big gray slab with holes in it. It's a breadboard and it will be one of your best friends in your prototyping future :)!
A breadboard is a prototyping-board that allows you to connect components without soldering them, allowing you to reuse them and rearrange them to taste.
A breadboard works with strips of metal plates below the holes which is best explained with an image
 
 
As you can see, the "+" and "-"-denoted lanes are connect all the way from the top to the bottom and the other lanes are connected horizontally on just the holes in that lane (usually 5). 
There is also normally a break in the middle, so the lanes on opposite sides are not connected.
This is why the black cable in the 123d-example above is connected to the resistor (the sausage-looking thing with colored stripes) without the cable actually touching it.

In the example we connect a regular 3v clock battery to a led and a resistor. We'll go through the components one by one

The battery


We're using a 3v clock battery, formally called a button cell. Button cells are great for long running components due to their low self-discharge. So they won't run out while not used.
Ours is a 3 volt one, which is pretty standard.

The LED


A light emitting diode is as the name suggest, something that emits light, when supplied with a charge, it glows.
The diode part of the name is pretty significant, a diode is a semiconductor (https://en.wikipedia.org/wiki/Semiconductor​) which can only conduct in one direction (assymetric conductance).
What this means is that the direction we plug this component in counts. It will only work if we plug the + side to the Anode and the - side to the Cathode. On LEDs the anode is usually the longer leg and the cathode side is flat on the "bulb".
And LED is usually not operated at high current, which is why we need our third component.

The Resistor

-Resistance is f..fruitful!
 
Resistors are used to lower the current of a circuit, a great question that we got during the code night is why the resistor was placed "after" the led, and the answer is that it does not really matter. 
You can place it before or after a component or at the end or beginning of the circuit, as long as it is in series with your the load that you want to lower the current for. Resistors work abit different in parallel. 
I wont go in on the details of it but it's important to know if you ever find yourself in a situation where you'll be putting resistors in parallel. You can read up more on it here: (https://en.wikipedia.org/wiki/Series_and_parallel_circuits)

The current is calculated with Ohm's law (https://en.wikipedia.org/wiki/Ohm%27s_law) which states that the current is the potential difference between two points in the current(charge in volts) divided with the resistance(in ohm Ω)
Ohms law can be deduced to I=V/R  where I is current, V is charge and R is resistance
The operating specs for the LED says that it's happiest with 20milliampere and that its load is 2.2 volt.

Since charge is the potential difference between two points, we calculate the needed resistance with this formula: R = (Vsource - Vled)/Iled where Vsource is 3 volt (our battery), VLed is 2.2 volt and Iled is 20milliampere 
R= (3-2.2)/0.02 = 40 Ohm and we get a nice somewhat 0.044 watts out of our LED :)!

The stripes on the resistor denote the resistance, but is based on an infuriating system which relies on you having a fantastic memory. So just go ahead and use a calculator, like this one: http://www.hobby-hour.com/electronics/resistorcalculator.php​
Enter the resistor value and press "display resistor" to get a visual representation of it, or select the different bands and "calculate" to see the resistor value. 
The last band is "tolerance" which is a percentage of how far away that resistance can be from the denoted value.



Hardware programming you said?


I apologize for the long and tedious introduction, but as David Allan Coe put it, "It is not the beauty of a building you should look at; it's the construction of the foundation that will stand the test of time".
Let us deep dive into the HIGHs and LOWs of hardware programming (pun intended). 

Puns and pins!

We'll start with GPIO. The gpio is the header pins of raspberry pi and arduino boards these can "read" and "write" a signal, high (on) and low (off). There's also a special trick called PWM that we'll get into later.

The pins are normally numbered, and some special pins exist. The common special pins on most boards are 3.3v and 5v pins, which supply the respective charge. GND, which is ground and TX/RX which is serial output and input.

Blinkenlights!

For our first real hardware programming example we'll extend our previous 123d circuit and add an arduino rev3, you can go to the example here(https://123d.circuits.io/circuits/1134942-led-with-arduino).


Instead of the battery +-side we'll connect one of the pins without the squiggly line (~), this is the symbol for PWM, which we will get in to later. For the example I chose pin 2.
We'll connect the negative or ground wire to GND on the board.

A really important thing to bear in mind is that the output voltage of the arduino pins are 5 volt and the raspberry pi ones are 3.3v, so we need to adjust our resistor accordingly!

Arduino

Arduino uses a heavily modifed version of C and uses two functions, setup() and loop().
Setup will be run once at startup and loop will be called consecutivly and only stop when you stop the program.

We'll dive right in to the code of the blinking LED
---------------------------------------------------------------------------
   
    int led 
<span style="color: #808030;">=</span> 

<span style="color: #008c00;">2</span><span style="color: #800080;">;</span>

// the setup routine runs once when you press reset:
void setup()

<span style="color: #800080;">{</span>


<span style="color: #696969;">// initialize the digital pin as an output.</span>

pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop()

<span style="color: #800080;">{</span>

digitalWrite(led, HIGH);

<span style="color: #696969;">// turn the LED on (HIGH is the voltage level)</span>

delay(1000);

<span style="color: #696969;">// wait for a second</span>

digitalWrite(led, LOW);

<span style="color: #696969;">// turn the LED off by making the voltage LOW</span>

delay(1000);

<span style="color: #696969;">// wait for a second</span>

}

---------------------------------------------------------------------------
In the setup we call pinMode() which sets a pin to OUTPUT or INPUT based on your need.
In the loop we write HIGH (on) to the led-pin, then wait for a second, then write LOW (off) to the led-ping and waiting yet another second. Which gives us a blinking light.

digitalWrite might sound complicated, and I personally find it a wierd name for something that only writes 1 or 0, or reads 1 or 0 (in the case of digitalRead).

Raspberry pi

On the PI-side I'm sticking with raspbian and python, but might update the examples with windows 10 IoT-core and c# later.
----------------------------------------------------------------------------
    import RPI.GPIO 
    as GPIO
import time
led 
    = 
    4
GPIO.setmode(GPIO.BCM)
GPIO.setup(led,GPIO.OUT)
while(True):
    GPIO.output(led,True)
    time.sleep(1)  
    #sleep in seconds
    GPIO.output(led,False)
    time.sleep(1)
----------------------------------------------------------------------------

The raspberry pi examples has some extra things that we'll need to cover. Firstly, it needs to import the GPIO-class from RPI and it needs to import time to do delays.
This due to the fact that python is not a specialised language for rasperry pi or hardware programming, like the heavily modified arduino C-language
Secondly we need to set the pin-layout, we set this to BCM, which gives us this layout:
Which means that to use gpio-pin 4, we need to plug it in to the board pin 7. 

on the raspberry pi we will have to supply our own loop in the form of a standard While-loop.


This ends the first part of the series on hardware programming, stay tuned as I will rapidly post sequels :)!
Live long and prototype!

Shameless plug: Are you a ninja? We are hiring in all our offices.

Share post