Skip to content

6 - Embedded Programming


How To Make A Computer

An intoduction to digital electronics

Interactive explanation on how computers work

Getting inspired by the past


Click on the image to download the PDF

Representing information

Now, let’s start representing information with our very basic circuit.

A couple of questions

  • How much information are we able to represent with this circuit?
  • How many possibilities do we have? And what if we combine all the circuits we just built?

Digital Logic in an Analog World

As we saw before:

A single switch can be on or off, enabling the storage of 1 bit of information. Switches can be grouped together to store larger numbers. This is the key reason why binary is used in digital systems.


With a bank of eight switches we can store 28 = 256 possible numbers. Each of those switches is called 1 bit, and 8 of them are a byte:


This implies then, that to represent information with electricity, we have one very important limitation: information becomes discrete:


Of course, depending on the amount of bits we use we have a more precise curve:


And if we put enough of them, we can represent more complex things like this:


Logic Levels

Now that we have talked about representing information with single bit, we have to agree on how things are going to talk to one another. For this, we standardise what we consider as a 1 (high voltage) and 0 or low voltage. We call this Logic levels:


The core component: Transistor

Wouldn’t it be ideal if we had a tiny mini switch we could control by ourselves? The transistor is nothing else than an electronic switch.

BJT symbol NPN.svg


And it basically works like this:


We will see how this transitor helps us build more complex things, by controlling how they are connected together.

Putting things together: circuits


Combinational vs sequential

Circuits can be combinational, when they don’t have any clue about their own state (like a mechanism):


Here you have them being used in a Marshall Amplifier:


But they can also be sequential, when they are aware of their state:


In combinational logic, the output is a function of the present inputs only. The output is independent of the previous outputs.

Sequential logic is the form of Boolean logic where the output is a function of both present inputs and past outputs. In most cases, the output signal is fed back into the circuit as a new input. Sequential logic is used to design and build finite state machines.


Truth table for 1-bit Full Adder (above):


Hands on

Let’s make these first combinational circuits (choose one or both):

AND gate

OR gate


Going integrated

The Apollo Guidance Computer:

With some integrated circuits in flat-pack design:

Agc flatp.jpg


Now IC’s come in tons of flavours:


And then they become programmable:

Early computing machines were programmable in the sense that they could follow the sequence of steps they had been set up to execute, but the “program”, or steps that the machine was to execute, were set up usually by changing how the wires were plugged into a patch panel or plugboard.

A stored-program computer is a computer that stores program instructions in electronic memory.

By Parrot of Doom - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=8318196

A microcontroller (MCU for microcontroller unit) is a small computer on a single integrated circuit. A microcontroller contains one or more CPUs (processor cores) along with memory and programmable input/output peripherals. In 1974 Intel released the first microcontroller, the Intel 4004:

Intel C4004.jpg


It was a revolution (specially for printers 😂):


And from them on:


The Arduino project

The importance of the freedom to use, understand, modify and share your tools.

  • Hardware - Programming IDE and libs - Community

Coding languajes

A high-level programming language is a programming language with strong abstraction from the details of the computer.
A low-level programming language is a programming language that provides little or no abstraction from a computer’s instruction set architecture.

From wikipedia


Algorithms and flow charts

[…] an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation. Algorithms are always unambiguous […]

To represent algorithms we can use a for of diagram called flow chart.

A flowchart is a type of diagram that represents a workflow or process. A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task.

The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows. This diagrammatic representation illustrates a solution model to a given problem. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.

From wikipedia


There are a lot of different symbols you can use on your flowcharts, these is the basic set and its function:


References

  • You can draw your flow charts online with draw.io

I/O Programming

Our code will interface with the outer world, receiving information, i.e. inputs, and actuating in some way, i.e. outputs. After receiving information from the environment, normally through some kind of sensor, we process the collected data to take decisions and actuate.

A balancing robot is a good example of this technic.

Practical coding

Variables

Think of variables as virtual drawers where you can store your stuff!


You can even label them with names, so you don’t forget whats inside. But remember to tell the computer what the content will be, so she can build the drawer big enough to fit it.

bool hello;
byte my;
int friend;
float how;
long are;
char you;
String today; // Only arduino!

Coding control flow statements

We can use control flow statements to manage how our program reacts to different inputs and actuates on the outputs.

Conditionals help us take decitions, remember that your question can only be answered with true or false.

if (condition) {

    // Do stuff

}

We can chain many conditions one after the other:

if (condition) {

    // Do stuff

else if (condition2) {

    // Do stuff 2

} else {

    // Do other stuff

} 

Loops can be used to repeat operations based on diferent parameters:

while (condition) {

    // Do stuff

}
for (# iterations) {

    // Do stuff

}

Comments

It is easy to forget what a piece of code was supposed to do if we read it after a couple of days, to write reminders or to explain what the code does, or simply to make a joke, we can use comments. In C language, simply starting a line with // will make the compiler ignore all the content on it.

// This line will be ignored by the computer!!

if (fail) {

    // Failure is the way to learn...
    print("ERROR!!!");

}

Functions

When our code starts growing, it can get very messy and difficult to understand. Functions are a way to organize our code by grouping instructions that we use often and then calling them. We can assign any name we want to functions, the idea is making it descriptive so we remember what the function does.

// We define a simple function to temporary light a led
void blink() {
    digitalWrite(LED_PIN, HIGH);
    delay(500);
    digitalWrite(LED_PIN, LOW);
}

// And we can use it whenever we want!
void loop() {
    if (ERROR) {
            blink();
    } 
}

Libraries

Libraries are sets of tools that someone has built to make certain tasks easier, for instance, reading pins in a microcontroller, or controlling the LEDs. For using our LED strip, we will need to install one library.

References

Barduino 4.0

This week we are going to be using the Barduino 4.0. Please check the documentation here

barduino4

And you can find the here also the Adventronics Calendar

Arduino Programming

Installation

You will need to install the Arduino IDE. After that, we need to define the board that we are going to use.

Guide

To get started with the Arduino IDE, and install the boards needed for the ESP 32 and the SAMD11, please:

Open your Arduino. Go to: Preferences > Additional Board Manager URLs

There, add the boards sources. One line per URL:

Paste this in the field there:

https://dl.espressif.com/dl/package_esp32_index.json

https://raw.githubusercontent.com/qbolsee/ArduinoCore-fab-sam/master/json/package_Fab_SAM_index.json

Hands on

Three things to remember

  1. Comming in or going out? pinMode()
  2. Pin on/off with digitalWrite()
  3. Read button state with digitalRead()

Reading a button

Digital input with DigitalRead()

int buttonPin = 2;
int value = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
}

void loop() {
  value = digitalRead(buttonPin);
  Serial.println(value);
}

Blinking led

Digital output with digitalWrite() using delay function.

void setup() {
    // Set pin 12 as output
  pinMode(12, OUTPUT);
}

// Loop runs forever
void loop() {
    // Voltage up in pin 12
    digitalWrite(12, HIGH);
    // Wait 200ms with pin 12 HIGH
    delay(200);
    // Voltage down in pin 12
    digitalWrite(12, LOW);
    // Wait 200ms with pin 12 LOW
    delay(200);    
}

I/O

If you are feeling adventurous, try this

// Our variable for checking if it's pressed or not
bool pressed = false;

// the setup function runs once when you press reset or power the board
void setup() {

  pinMode(12, OUTPUT);
  pinMode(27, INPUT);
  // Turn the LED off
  digitalWrite(12, LOW);
}

// Loop runs forever
void loop() {
  // Read the pin
  if (digitalRead(27)) {
    // pressed!
    pressed = false;
  } else {
    // not pressed!
    pressed = true;
  }

  if (pressed) {
    // Turn it on
    digitalWrite(12, HIGH);
  } else {
    // Turn it off
    digitalWrite(12, LOW);
  }
}

General resources