Third electronics-related lesson of the FabAcademy. This time big step: learning code. The goal? programming the board I have made previous week, including a switch and a LED, to do something.
Understanding the communication
Very important at this point is to understand the connections we are establishing between the board and the computer. They are:
- AVRISP connection: it powers the board and allows the computer to talk to it. It is through here that the code is loaded into the ATTiny.
- FTDI connection: it powers the board and allows it to talk to the computer. It is through here that the board communicates back with the computer and with us through the serial monitor.
Always install an FTDI header, if only to be able to calibrate the board or to debug if necessary. Then the header can be removed in space is needed.
The second image represents the schematic for the FTDI cable, which has defined the order of the pins in the schematic as seen in image 1.
Note: important not to plug both cables at the same time. Instead, plug and unplug ISP and FTDI cable from the same USB port for an agile programing and serial communication with the board.
In order to set up the board to communicate to the computer and work with the Arduino IDE, the next steps were necessary:
- Install ATTiny in Arduino environment as explained in this tutorial, so you can program ATTiny microcontrollers using the Arduino IDE.
- Install the Crosspack for AVR Development (see explanation here) with the help of this tutorial. This is the necessary platform for programing AVR microcontrollers.
- Install the VCP and D2XX Direct drivers for FTDI-USB using this tutorial (when done successfully something like "/dev/cu.usbserial-XXXXXXXX" should appear in the Arduino IDE under Tools > Port).
- VCP drivers can be found here. As easy to install as opening the package and following the steps.
- D2XX Direct drivers can be found here. As indicated in the cell specific for my OS X, you do not only have to install the drivers themselves, but also D2XXHelper to prevent weird errors from happening with newer versions of OS X. The D2XX drivers are trickier to install than the VCP, so download the .dmg and follow the instructions in the Readme.txt file. Be careful! since the steps for the terminal commands only work if you have copied the folders contained inside the .dmg and pasted them on your desktop. Also, the commands for the terminal don't all really match the directories of the files inside these folders, so adjust them (specifically the libftd2xx.1.2.2.dylib, that can be version 10.4 or 10.5-10.7 for newer versions). Apparently Emma recommends installing the 10.4 version.You could do all this copy paste by hand, for which you would need to access /usr/local/lib and /usr/local/include (both hidden from the Finder) through Go > Go to folder (Cmd+shift+G) in the Finder . I used the terminal commands to do the operations, but checked afterwards that the files were indeed pasted in the new location since the Terminal doesn't give any feedback about the commands being successful. Ufff...
- Make the AVR ISP cable for the communication between the ISP and the echo.hello-wold, making sure that wire 1 (white wire in my case) was aligned to the mark in both the headers.
- Find the right orientation of the communication PINS in both the ISP and the board (marking them with a dot), so one makes sure that every Pin is connected to its relative on the other board.
- Connect the ISP to the USB and the Echo.Hello_world board and burn the bootloader: Tools > Burn Bootloader. Make sure you have selected ATtiny as “Board”, ATtiny44 as “Processor”, 20MHz (external) as “Clock” and USBtinyISP as “Programmer”.
Everything should now ready to program the board from the Arduino environment.
Coding
First thing I tried is to upload the "blink" sketch from the Arduino examples to see if the board actually worked. For that, and given the fact that I am not using an Arduino UNO board, the first step was to redefine the pin that is connected to the LED. The following picture is an essential one for that purpose, and it is not included in the data sheet of the microcontroller. It establishes the relation between the pins in the schematic and the actual pins to be used in the code.
Having defined pin 7 as my the pin for the LED in the code (6 in schematic), I uploaded the sketch and the LED blinked with a second of delay. Yay! I tried modifying the delay to see if that also worked, and it did. I was ready now for including the tact switch into the equation and making both do something.
I had already worked with Arduino before and therefore I was somehow familiar with the structure and some of the functions, variables, libraries and so on. However, I decided to take the beginners approach and started from scratch by reading the book Getting Started with Arduino, written by one of the Arduino founders, Massimo Banzi.
As I advanced in the book I was going through different small incremental exercises used to explain different elements in the code every time (always including a LED and a tact switch). I wrote and coded everyone of them, and following you can find the exercises in the order that were done, a video demonstrating the interaction and the related code.
- Exercise 1: the LED turns on when the tact switch is pressed and it turns off when the pressure is released (video 1). The code, that can be found here, is pretty basic and includes only one variable for the digital input of the button and an if statement for the behavior of the LED. The functions digitalRead, digitalWrite and pinMode are introduced in this exercise. Video 2 shows the total opposite (LED is always on and turns off only when the tact switch is pressed) because, in my case, LOW as an input value means that the button is pressed. This happened because of the way the tact switch is connected to the microcontroler, reading the pin 0v (LOW) when the button is pressed.
- Exercise 2: the LED turns ON and OFF alternatively every time the tact switch is pressed, remaining in its ON and OFF state when pressure is released. In this exercise, the code (find here) deals with difficult aspects like refreshing the value of the digital input variable to avoid multiple reads when the button is pressed, and performing a different action depending on the state of the LED (turn ON if it is OFF, turn OFF if it is ON). In this exercise, the function delay() as well as the boolean operator && are introduced.
- Exercise 3: let the LED fade in and out. The objective was to understand the concept of PWM (Pulse With Modulation) in the simplest possible way. In the code (find here) the function analogWrite is introduced.
- Exercise 4 - Final exercise: a combination of both exercise 2 and 3, the intention was to have the LED switched ON and OFF when the button is pressed once, and increase its brightness over time if the button is pressed for more than half a second. In the code (find here), the function millis() is introduced.
Serial Communication
In addition to the previous sketch, I though it would be nice to try at least to establish a serial communication between the board and the computer.
In addition to the previous sketch, I though it would be nice to try at least to establish a serial communication between the board and the computer.
- Exercise 5: I started with a very simple code in which I eliminated all the fuss with the LED and the switch in order to stick to the serial communication. I basically made the board to say "Hola!" (hello in spanish) to the serial monitor every second. Find code here.
- Exercise 6: for the second trial, I integrated the communication into the sketch for Exercise 4, making the board write "LED on", "LED off" or "Dimming" depending on the state. Code can be found here.
Workfiles
Añadir un comentario