Skip to content

From physics to information


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.

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

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