09 : Input Devices<
Hero Shot<
Learning Outcomes<
What did I learn this week ?
- Hardware debugging :
- Unsolder a microcontroller
- Resolder a microcontroller
- Good resistor value for UPDI to FTDI
- Software :
- Read an analog value with Arduino IDE
- Plot an analog value with Arduino IDE
- Change the vertical scale of the Arduino IDE plotter
- Transistors
- Reminder about transistors
- How phototransistors work
Assignments<
This ninth week's asignments are :
- Group :
- Probe an input device's analog levels and digital signals
- Individual :
- Measure something: add a sensor to a microcontroller board that you have designed and read it
1. Individual Assignment<
1.1. Discussions about the final project<
Since my final project will need input devices, I decided to directly work on it for this week's assignment. Therefore I discussed with Nicolas about what I would like to do.
Shortly, my final project would be something that maintain a water level in a water tank used to generate fog. The flow rate would be really slow (around 0.1 mL/s or 1mL/min) and I would need around 1mm precision on the water level control. At first, I was thinking that using an electronic water level sensor and a peristaltic pump would be the best way, however after discussing a bit with Nicolas, we realised that something purely mechanic would be simpler and maybe more precise.
He thought about a mechanism sometimes used to water plants.
I then needed to start thinking about a B plan for my final project : it would be great if we can measure the fog density during our experiments. Nicolas suggested an optical method that uses à phototransistor and a laser (more details on my final project page).
1.2. Reparing my board<
First of all, since the assignment is to use our own PCB, I had to repare mine. To recall, the microcontroller was burnt last week. I will try to unsolder it and to replace it.
I first tried to unsolder it with a classic soldering tip but it was too hard to keep each pin hot at the same time. Therefore I used a heat gun.
I managed to detach the broken microcontroller but some of the copper track are a bit peeled off (but still usable I think).
It was not an easy task to solder a new microcontroller on peeled off tracks but I did it :
1.3. Circuit<
Design<
The idea of the circuit is the latter :
The phototransistor's emitter is grounded, the collector is connected to the VCC through a resistor. If there is no current, there is no tension through the resistor and therefore the collector is at VCC. When the phototransistor receives light, it creates current from its collector to its emitter, hence through the resistor. The latter creates a tension difference between its two pins and the collector's tension is VCC diminished by this difference.
Measuring the collector's tension indicates how much current is created by the phototransistor and hence how much light is received by the latter.
Phototransistor<
I'm using a L-53PC3 phototransistor. Its datasheet indicates a maximum accepted voltage of 30V and the AtTiny412 power supply is 3,3V so I don't have to worry about the resistor I choose.
Building<
I built the circuit on a breadboard :
and connected the phototransistor collector to the PA2 pin of the ATTiny :
1.4. ATTiny412 Programming and Communicating<
Code<
Here is the Arduino-C code I wrote to read the PA2 pin tension value :
int sensorValue;
void setup() {
// initialize the sensor pin as an input:
pinMode(A2, INPUT);
Serial.begin(9600);
}
void loop(){
// read the state of the sensor value:
sensorValue = analogRead(A2);
Serial.println(sensorValue);
}
I will use Arduino IDE to program and communicate with my microprocessor.
Sending the code<
First in first, I installed the board on Arduino IDE. One has to install megaTinyCore by Spence Konde to work with ATTinies :
To recall, my board was designed in order to be able to program and communicate with it using a USB port and a FTDI converter. I then connected the board to my computer using such a cable :
Once connected, I went to the Tools menu and as usual selected the board and the used port. However this time I also have to select a Programmer and as I'm using a serial UPDI with a resistor, I selected this one :
4,7k Ohms resistor is required for UPDI to FTDI !!!
At first it was not working. I got the following error message :
pymcuprog.pymcuprog_errors.PymcuprogError: UPDI initialisation failed
Failed programming: uploading error: exit status 1
I tried a lot of debugging and I couldn't understand why it was not working. I let Axel worked on it and he understood that I was using a too low resistor in the UPDI splitting in the RX and TX pins of the FTDI converter. Indeed I put a 220 Ohms resistor and a 4700 Ohms is required (as you can see above in the arduino programmer description...).
Axel changed my resistor and told me to verify if it was working.
Now that the good resistor in soldered to my board, it was working correctly and my code was uploaded in the ATTiny412. However, I can not see anything yet in the Arduino IDE since the my board is connected to my computer through its programming pin.
Receiving the data<
To communicate with my board, one has to connect the FTDI converter through its other side (the one linked to the RX and TX pins of the microprocessor). However, I inverted the RX and TX pins hence I have to use cables between my board and the FTDI :
The yellow and green are crossed to repare my mistake.
It works ! The data are coming to my computer and I see them through the Arduino IDE Serial Monitor. It is of course more convenient to use the Serial Plotter and below you can see that it works :
In order to create such visible peaks, I had to do very fast pulses with my phone flashlight, by passing in front of the phototransistor very rapidly. Furthermore, as soon as I stopped doing pulses, the plotter zooms on the signal and the noise seems very large. It is hard to distinguish clearly noise and signal.
Therefore I modified a bit my code by simply adding print instructions to create constant lines at the maximum and minimum values :
int sensorValue;
void setup() {
// initialize the sensor pin as an input:
pinMode(A2, INPUT);
Serial.begin(9600);
}
void loop(){
// read the state of the sensor value:
sensorValue = analogRead(A2);
Serial.println(sensorValue);
Serial.print("1000.0, ");
Serial.print("0.0, ");
}
If I do light pulses again, it looks like that :
2. Group Assignment<
To do