fabacademy


Week 8, Embedded Programming

In the Covid-19 era, working and studying has never been so hard and unmotivating. If I think of FabLab and IaaC I almost get emotional. I left Barcelona to come back to Italy due to my lung condition and to be sure I would get the right medical assistance if I ever needed it. But before leaving the sunny Barna, luckily I bought an Arduino UNO board, and that's what I'm going to use to practice and do my week's assignments. As a total noob, will it be challenging? Fun? Frustrating? Boring? Well, let's make it cozy and blink some LEDs ( that's all I have :( )

If you are programming a board you should not only be able to feed it the right codes and connections for your purpose, but should also know something about how it works internally. This is totally not my case, so I tried to understand more of the microcontroller my UNO is mounting. It is the ATmega328P by Amtel.

ATmega328P_Datasheet


blablabla

Reading its data sheet the information I can get is too technical for my knowledge, but I can grasp some basic concepts. I understand it works at 8-bit, so it can process/transfer 8 bits of data at the same time. It’s developed with advanced RISC architecture (???).
Let’s google this… Reduced Instruction Set Computer, I read it means the set of instructions it uses is smaller and more optimised than those of other microprocessors, this makes me believe that it could make it faster. The memory of the unit is non-volatile, meaning it will be stored even when not powered, holding the data up to 25 years of time. It has 23 programmable lines, that can work both as inputs or outputs and works between 1.8V and 5.5V. Anyway, we normally feed it the 5V of the USB port.

Now to (noob) programming!

We have programmed boards earlier in the master, both for Useless Machines and Unpacking Intelligent Machines, but after that I never focused on it, and never owned a board. This occasion made me buy my first Arduino, which I hope will be a great mate for my future.

Since I previously used the Arduino IDE, I started setting up my board with it and tried some basic examples already provided. To test the board I opened the Blink code, set everything up and uploaded it. I never remember that LEDs have a direction and had to switch it, then everything worked fine.

blablabla



For the second function I opened Fade, to test analogWrite, this worked fine as well.

blablabla



I then tried the BlinkWithoutDelay sketch. I understood it works with the internal clock of the board, so that the board continuously checks the time passing to understand if it’s time to change the state of the LED. This is interesting because in this way you can run your led while letting the board execute other functions that delay would stop.

Last, I wanted to use a potentiometer to control the brightness of the LED, which I managed to implement thanks to this simple tutorial

LINK TO THE TUTORIAL



sketch file


The code is reading the voltage that comes out of the potentiometer, and then assigning a brightness value. Since the value is proportionally assigned from a range of 0 to 1023 to a range of 0 to 255, the output values are not linear but very steppy, or maybe it is because of the low quality and low precision potentiometer I’m using. Also, I didn’t like the fact that turning the knob clockwise was decreasing the brightness, so I simply inverted the values 255, 0 to 0, 255 in outputValue.

To program with registers I first had to understand what registers are. I discovered it means programming for the port registers directly, instead of the single pins. This makes it more complicated, since the code is more difficult to understand, but at the same time allows you to set multiple pin states with a single line command.

This link

I AM THE LINK

helped me understand with a very easy explanation. I then tried to program some simple led blinking with registers inside of PlatformIO, which I installed on Atom.

blablabla

blablabla

First, I used one of the simple codes on the link to have a simple blink.

blablabla

Then I modified the code manually to make the leds light up with a pattern.

blablabla



Pattern PlatformIO project download

After this simple testing I’m becoming very interested in the possibilities simple boards can offer. I start from a very low level of knowledge so felt happy of making some basic blink, but I will surely explore more during the input and output devices week. This has been a good way to get familiar with Arduino IDE and understand the basic rules of its coding language, after observing the logics behind the already made codes I used.