Week 08. Embedded programming

Goal

The Goal of this week is to learn to read a datasheet (in our case we are reading the Atmel ATtiny44A) and program the hello-echo-board we made 2 weeks ago to do something in as many programming languages as we can.

Last Week MIT lecture review

If you ask me to imagine what could be the worst possible day in the whole year for the script to pick me I would say: last wednesday is that day.

MURPHY'S LAW:  "Anything that can go wrong, will go wrong."

From all possible and available weeks so far, all of which I successfully completed my assignments, the damn script, which I hate so much already, did not find a better day to pick me but last wednesday.  During my showcase Neil recommended me to start over again with a simple shape going through all the steps. I think that since it is my very first molding and casting, Neil gave me quite an intelligent advice.

After the pause Neil talked about embedded programming. I can't tell you anything about it because I did not understand a single word. I don't know the rest of the class, but when he finished there were no questions and all the labs were quiet. I was just praying that Bas in Amsterdam had been recording the lecture. We were experiencing connection drops every 15 minutes because it was raining. Here in Barcelona when it rains not even the traffic lights work.

Atmel ATtiny44A Datasheet

I'll do this first because it looks is sooo boring. 258 pages of pure information. No literature here. Not a single useless phrase or word. There is no cover, no welcome, or introduction, it begins straight to the point with information. The first I read in the cover is that FLASH memory has only 10.000 write/erase cycles. I thought these chips would last forever under normal conditions. Then I looked in the datasheet for FLASH memory (I did not remember what was it) and I found it that it was for program storage. So you can only upload 10.000 times a program to it. Well, to be honest I think it should be enough. I started hyper-jumping up and down through the pages to see if I landed in something interesting. While I browsed the datasheet I wondered who could write all of this stuff. The second time I stopped was in page 56 when I read code example, this might be useful. The datasheet is full of code examples both in Assembly and C. Assembly looks really difficult. There is not even an Assembly for dummies book. That ultimately means that dummies should not learn Assembly.

From that moment I stopped reading the datasheet and I started using it as a reference book. The datasheet is not meant for reading as you read a novel but to extract information from it.

Hands on with the Arduino IDE

Thanks to Mark Sproul (who made the Arduino core portable across processors) and a tutorial from the High-Low Tech research group at MIT Media Lab, it is now possible to use the Arduino software to program the ATtiny. Even though it has quite limitations, it is a good starting point if you are used to the Arduino programming language.

Step 1: Making LEDs Blink

 I have 2 LEDs in my board. This program below (sketch as Arduino software names it) should turn each LED on and off alternatively, every second.

BLINKING LEDS PROGRAM:

/*
Blink Turns on an LED1 on for one second, then LED2 on for one
second, repeatedly. This example code is in the public domain.
*/
// Pins 7 and 8 correspond to legs 6 and 5 in ATtiny44A.
// give it a name:
const int led1 = 7;
const int led2 = 8;
// the setup routine runs once when you power the board:
void setup() {
// initialize the digital pins as outputs.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT); }
// the loop routine runs over and over again forever:
void loop() {
// Turn the LED1 on (HIGH is the voltage level)
// and LED 2 off
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(1000); // wait for a second
digitalWrite(led1, LOW); // Alternate LED states
digitalWrite(led2, HIGH);
delay(1000); // wait for a second
}

From the datasheet I read that the ATtiny44A has 4K byte of FLASH memory for program storage. When you verify the program in Arduino IDE, it gives you this message: Binary sketch size: 902 bytes (of a 4,096 byte maximum). So this simple program uses almost 1/4 of the ATtiny storage capacity. To load this program into the board you have adjust the Arduino IDE parameters. Set the board to program as an ATtiny44A (20 MHZ), in TOOLS>BOARD>ATtiny44 (External 20 MHZ Clock), and if you are using the FabISP as the programmer set it to USBtinyISP in TOOLS>PROGRAMMER>USBtinyISP. Pins 7 and 8 have been extracted from the tutorial of the High-Low Tech research group, this information is not in the datasheet, look at the image below:

ATtiny44
Pins of the ATtiny44A for use in the Arduino IDE

Before uploading the program (=sending it to the microcontroller and spending one of the 10.000 write/erase cycles), remember to power both boards (the FabISP with the USB cable and the hello-world board with the FTDI cable). If you don't power both boards you'll get a connection error when uploading your program. I successfully uploaded the program into the microcontroller but the Light Emitting Diodes did not emit any light.

In search of the error

I checked with a multimeter that the board was being supplied with 5V Vcc. The datasheet in section 20 indicates that there is  maximum operating voltage of 6V and a range of Vcc the microcontroller can deal, but Vcc is also related to the maximum operating frequency of the device. Also pins PB2 and PA7 (8 and 7 in the sketch) were supplying 5V and 0V every second approx, so the microcontroller was working. Output pins can only supply 40 mA. So the problem should be on the LEDs. I checked them with the multimeter and I noticed that they were giving me continuity. This shouldn't happen. I revised the design of the board I saw there was an error when I designed it, and the anode and the cathode of the LEDs were connected by a wire. So I desoldered the LEDs by quickly alternating the soldering iron between the pads and confirmed the error. Check the pictures below:

ATtiny44
Left: Design error: Anode and Cathode pads connected. Right: Wires removed

I removed the wires with a sharp blade and soldered the LEDs again. While I was removing one of the LEDs with the tweezers I heard a click and that was the last time I saw it. It might went to the Twilight Zone. Relax with the tweezers. So I replaced it with another LED from electronic waste I had in my basement. It's not the same model but who cares.

I connected the board to the FTDI cable again but no light was coming out of the LED's. The LEDs were supposedly connected in the right way because I connected them following an image I found in the Internet: This image to be exact:

Wrong image
This image is partially wrong.

Later I found several other images showing the opposite. Do not trust blindly everything you find in the Internet. A good practice is to find at least 3 sources of information stating the same. The image above is wrong. In reality, the T points to the cathode and also there are green marks in the top of the LED that indicate the cathode. So finally I unsoldered and soldered the LEDs the right way this time and when I connected the FTDI cable the program was working!

Wrong image
Blinking LEDs. The red board on the right is a Sparkfun Basic FTDI (same as the FTDI cable)

Step 2: Using the SoftwareSerial Library to send messages to the computer

In order to use the SoftwareSerial library and communicate with your computer you have to follow some steps:

  1. Download and install the VCP (Virtual COM Port) FTDI Driver for your operating system.
  2. Set your board in Arduino IDE either as 8 MHz internal clock or 20MHz external clock. Every time you change this setting you have to set the fuses in the board in TOOLS>BURN BOOTLOADER. SoftwareSerial library will not work if the microcontroller is set to run at 1 MHz internal clock.
  3. At the beginning of your arduino sketch include the SoftwareSerial library (It will take a bit more than 2K bytes of program storage) and declare the RX and TX pins of the microcontroller. Look in the FTDI board in the image above,  the wires go to pins 0 and 1 in the microcontroller.
    #include
    SoftwareSerial Serial(0, 1); // RX, TX
  4. In the void setup() section of the sketch, initialize the serial connection:
    Serial.begin(9600);
  5. In the void loop() section of the sketch, every time you want to send a message to your computer use the println function:
    Serial.println("LED 1 ON");
  6. Once you have uploaded your program to the board, set your serial port in TOOLS>SERIAL PORT. In my case /dev/cu/usbserial-A100RTDG and open the Serial Monitor in TOOLS>SERIAL MONITOR and . If it gives you an error, try again with another port.

Eventually you will start seeing in the Serial Monitor the messages you have set. Your board is now talking to your computer.

Wrong image
echo-hello-world board talking to the computer. Notice the size of the program has increased by more than 3x. Get this sketch in the Downloads section below.

What I learned

The Internet. A global system of interconnected computer networks . A network of networks. Thanks to it we have now access to an exponential growing source of fresh information. Without centralized governance, open for you. I can't imagine my life without the Internet. Most of the things I have learned in the last decade, have been learned there.  And the latest thing that the Internet taught me this week is that you should not blindly trust all what your read in the Internet.

Download files

You can download all the files related to this week here.