This week I aimed to use an input that would have a role in my final project. I hoped to use a tilt switch to relay whether or not the buoy was off balance, thereby indicating a point scored.
The first step was to discover how the tilt switch worked with a breadboard, and LED and a 9V battery. I used a 560 ohm resistor and jumpers to create a circuit from the battery to the tilt switch. When the tilt switch was facing vertically, the LED stayed lit. However, when I rotated the breadboard (with the tiltswitch attached) the LED went out – The ball inside the tilt switch had moved out of position, breaking the circuit.
I then used the Arduino instead of the battery, using the 5V and GND pins to power and close the circuit.
This weeks assignment is to use only inputs, so I set to work on a feedback loop happening internally in the arduino. The tilt switch would remain on the breadboard and be connected to analog pin A5 and 3.3V. I then wrote the following code to give feedback within the Serial Monitor,
void setup() {
Serial.begin(9600);
}
void loop() {
if(int tiltValue = digitalRead(A5)) {
Serial.println("ON");
}
else{
Serial.println("OFF");
}
delay(500);
}
The program successfully types 'On' or 'Off' depending on the position of the Tilt Switch.
I needed this same interaction occur on an ATTiny84 on the breadboard, an equivalent microcontroller for the one I would solder.
On reading an article on the High Low Tech website, I learnt the first step was to program the Arduino. Without any other wires, I connected Arduino with USB and chose programmer 'ArduinoISP'. I opened the Examples > 11. Arduino ISP > Arduino ISP and uploaded.
Secondly, I need to use the same tilt switch program to the ATTiny on the breadboard. I did this by reviewing the datasheet, in order to know which legs of the ATTiny should be connected to which Arduino pins, and its orientation.
I also placed a 10Uf capacitor (50V, not 25V because unavailable) between the Reset and GND on the Arduino. According to highlowtech.org this is because it prevents the board from “resetting (which starts the bootloader), thus ensuring that the Arduino IDE talks to the Arduino ISP (not the bootloader)”.
Initially, the program displayed the following error. I tried running the 'Bare Minimum' example program but it also failed to upload.
I then tried to use the cable on the FabISP, but was not successful. Despite this, I moved on to making an equivalent board.
In Eagle, I layed out a series of components in a similar fashion to the Echohello board. To position the tilt switch, I selected a PINHD-2X2-SMD from the fab library. I used the milling layer to lay out a rectangular dimensions and exported the files as pngs.
The cut file was later inverted to white in Photoshop as per the Electronics Design assignment. The pngs were then re-exported to Fab Modules with the same settings.
On the SRM-20, I again took an average z-position of the bottom-left, middle and top-right of the board. The first cut left some residual copper between the AT-Tiny – I swapped to a new blade and it cut perfectly. I then ran the cut file, which produced an additional border but did not damage the traces.
I then soldered my components one by one,
I used a light pair of grip plyers to bend the legs of the title switch toward the four pads. It was at this point I felt the tilt switch (from an Arduino starter kit) was not functioning correctly. Having checked with a multimeter, it appeared the its connection happened diagonally, not parallel as with the pads I had routed. I later discovered they were in fact parallel. The legs of the tilt switch were not long enough to bend to the pads, so I had to consider alternatives. No SMD button in the Fab Lab would be wide enough to stetch over the trace below, so I chose to solder a through-hole button. A final check with the multimeter proved that it and the rest of the components were soldered correctly.
Settings in Arduino IDE (Under Tools)
I plugged in the Arduino USB without any other wires. I then uploaded Examples > 11 Arduino ISP > Arduino ISP
avrdude -c usbtiny -p t44
You can used the following code within the Mac Terminal line to detect your board. My board was not detected at this point, so I proceeded strictly with Arduino IDE.
Settings in Arduino IDE (Under Tools)
It was necessary to install a 10Uf capacitor as with the breadboard. I connected the wires as follows,
ATTiny Value | ATTiny Pin | Arduino |
---|---|---|
VCC | 1 | 5V |
GND | 14 | GND |
RESET | 4 | 10 |
MOSI | 7 | 11 |
SCK/SCL | 9 | 13 |
MISO | 8 | 12 |
Under Tools, I selected 'Burn Bootloader'. Initially for the Input assignment I was not successful in burning the bootloader. I found it necessary to check the pinout diagram as outlined in the ATTiny datasheet (I have placed an image in my output assignment page).
An observation from Emma was that I had a 220 Ohm resistor for my switch (button) – 10k resistors are usually more applicable in this case.
I uploaded a basic program to initialise input as follows,
#include **SoftwareSerial.h**
#define rxPin 0
#define txPin 1
SoftwareSerial serial(rxPin, txPin);
int pin_B_sensor = 3;
int value_B_sensor = 0;
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(pin_B_sensor, INPUT);
serial.begin(9600);
}
void loop(){
value_B_sensor = digitalRead(pin_B_sensor);
serial.print("the status of the button is: ");
serial.println(value_B_sensor);
delay(1000);
}
The code was written in C, a standard (but not the only option) in Arduino IDE. The progam references the analog equivalent of the Tx and Rx pins on the ATTiny. The button/tilt switch defined as an input and if there is a connection (value_B_sensor = digitalRead(pin_B_sensor)) it prints its value in the serial monitor.
I attached the FTDI cable to my board using the following guide – a useful reference from Emma was that the side with GND is connected, while the other is not. The other side of wire I connected to the Mac's USB port.
I initially did not get the serial port to appear – The Serial Monitor didn't even open. I considered it might be a Mac issue (I am OS X Sierra 10.12.6). I had also installed the following driver – I therefore uninstalled as per the documentation. Thankfully, it was neither of these.
After confirming my FTDI wiring setup was correct with with Emma, I reinstalled and shutdown my computer. Warning: A restart is not enough to get the serial to appear in /dev/ and Arduino IDE, you need to shutdown.
After several weeks attempting this, it appeared there was something wrong with the board. Possibly I had short-circuited it along the way. I created another echo hello board, this time with a tilt switch attached. With Vincent, I reviewed its data sheet again, confirming how the pins were connected. As my soldering had improved, I was able to bend the pins and solder to wider distance pads. I started with a basic program that Vincent had outlined,
#include **SoftwareSerial.h**
#define rxPin 0
#define txPin 1
SoftwareSerial serial(rxPin, txPin);
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
serial.begin(9600);
}
void loop(){
serial.print("hello");
delay(1000);
}
The program simply refers to the analog equivalent of the ATTiny pins, and prints 'hello' in repeat. I uploaded the program using the same settings as burning the bootloader. When it uploaded, I change the port to USB (usbserialFT94TKFS – left Mac port). I then selected the Serial Monitor from the Tools > Port menu. The monitor read as the image above.
I selected the FTDI from the Tools > Port menu and then selected 'Serial Monitor'. The monitor read as follows: ON when the button was pressed, OFF when released.