We've gone from tretton37 to 13|37.

We’re still the same company with the same heart and focus on empowering clients and colleagues – and that will never change – but now we’re shorthand! If you want to get in touch to talk to us about empowering your organisation, uncovering your digital capabilities or find out more about life at 13|37 then head on over to 1337.tech.

tretton37.com will be available for a limited transition period only. Have a wonderful day.

Visit us at 1337.tech

Introduction to Hardware Programming (Part 1)

Posted 2015-10-26 By Otto Remse

In this blog post, 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 a bit 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 in any hardware! Go sign up here!

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 "deficit".
A real-life circuit is filled with negatively charged electrons flowing from negative to positive poles.
Some of you sciency-people might object that there are also positively 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 are best explained with an image

As you can see, the "+" and "-"-denoted lanes 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.


A light emitting diode is, as the name suggests, 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 which can only conduct in one direction (asymmetric 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 a bit different in parallel.
I won't 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.

The current is calculated with Ohm's 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 say that it's happiest with 20milliampere and that its load is 2.2 volt.

Since the 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.

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.


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.

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. I chose pin 2 for the example.
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 are5 volt and the Raspberry Pi ones are 3.3v, so we need to adjust our resistor accordingly!


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

We'll dive right into the code of the blinking LED

  int led = 2;
  // the setup routine runs once when you press reset:
  void setup()
    // initialize the digital pin as an output.
    pinMode(led, OUTPUT);

  // the loop routine runs over and over again forever:
  void loop()
    // turn the LED on (HIGH is the voltage level)
    digitalWrite(led, HIGH);

    // wait for a second

    // turn the LED off by making the voltage LOW
    digitalWrite(led, LOW);

    // wait for a second

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 weird 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
        #sleep in seconds

The Raspberry Pi examples have 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 Raspberry 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: We are a very unique family of driven software-creating professionals.

Join us. We are hiring in all our offices in 🇸🇪 Sweden & 🇸🇮 Slovenia.