# 10. Input devices¶

## Individual assignment¶

• Measure something: add a sensor to a microcontroller board that you have designed and read it

## Deciding which input device to use¶

The first part of the assignment is obviously to decide which sensors to use. Since my final project will include sensors, I wanted to design board for the sensors I was going to use. I decided to use an time of flight sensor. The sensor linked above is the one that Dr. Neil Gershenfeld recommended during this week’s lecture

Dr. Gershenfeld suggested that for any sort of distance sensing, we should use this time of flight sensor. He did, however, say that it was a good idea to use a breakout board with the sensor. He explained that since all the pads of the sensor are on the bottom and they are so close together, it starts making more sense to just use a board that already has the sensor.

This is the pinout for the breakout board I will be using:

I am going to use an ATtiny1614 for this board:

This is the pinout for the ATtiny1614:

## What is a Time of Flight (ToF) sensor?¶

A Time of Flight (ToF) sensor is a sensor that emits light at a frequency that humans can not see. For this specific sensor, the wavelength of the light is 940 nanometers (nm). When that light travels and bounces off something, those light beams will bounce back to the receiver part of the sensor. The sensor will then calculate the time between sending and receiving the message. Based off the time it takes, the sensor will then estimate the distance between it and the object. The VL53L1X has millimeter precision when finding distances. Since the speed of light is 299,792,458 meters per second (VERY FAST!), this sensor has to be immensely accurate and precise when calculating times and distances. After learning about how this sensor works, I was able to further appreciate the technology that a tiny sensor like this one can have. To learn more about time of flight sensors and how they work, visit this link or this link

In the image below, there is an equation that can help you find a distance like this yourself. In this equation, d is the distance calculated, c is the speed of light, t is the time between emitting and receiving the signal, and we have to divide everything by 2 to get the distance one-way.

## Testing out on a breadboard¶

Going along with the idea of starting simple adn suing progressively larger “circles” and testing each individual component before putting everything together, I started off by building this board on a breadboard

Going along with the idea of starting simple and using progressively larger circles, I started by simply blinking the built-in LED on the Arduino using this code. This allowed me to make sure that the Arduino itself worked well before heading to any other step:

``````void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
``````

### ATtiny1614 breakout board¶

After this very simple step, I wanted to blink an LED using the ATtiny1614. I had never used this board before, so it was definitely helpful to get started with some of the basics. Since the ATtiny1614 chips we had in our lab are surface mount, we used this breakout board to use them on breadboards.

Once this breakout board was soldered and had header pins attached, I could easily put it onto a breadboard. The first thing I had to do was upload “jtag2updi” to the Arduino to make it a programmer (as seen in week 06). Then, I had to connect 5V from the Arduino to Vcc on the ATtiny1614, ground to ground, and pin 6 on the Arduino to the UPDI pin on the chip, which is at the very bottom right on the breakout board.Then I had to connect an LED to Vcc and PA5 with a 330 Ω resistor in between. As you can see on the pinout for the ATtiny1614 at the top of this page, PA6 corresponds to digital pin 2. This means that when I refer to the pin at which the LED is located, I will say pin 2.

This is the code for blinking an LED on the ATtiny1614. As you can see, the only difference is that instead of having setting “LED_BUILTIN” as the output device, it is “2” for pin 2.

``````void setup() {
pinMode(2, OUTPUT);
}

void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
``````

### Using the sensor directly with the Arduino¶

After all those steps were done to make sure that the Arduino and chip were functioning correctly, I had to test out the actual sensor which is obviously the most important part. This github page has all the details about the sensor. It has all the libraries necessary, several different example codes, and thorough explanations of how to use the sensor and how to troubleshoot your issues. Below all the links is a section titled “Getting started”. The first subsection is just connecting all the correct wires and pins:

This what it looked like with all of those wires connects to the correct pins. The Arduino actually has 2 dedicated pins just for “SDA” and “SCL” which are in the corner close to pin 13. This code is the is what we upload to the Arduino to read values from the sensor:

``````#include <Wire.h>
#include <VL53L1X.h>

VL53L1X sensor;

void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000);

sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
sensor.setDistanceMode(VL53L1X::Long);
sensor.setMeasurementTimingBudget(50000);

sensor.startContinuous(50);
}

void loop()
{
if (sensor.timeoutOccurred()){ Serial.print(" TIMEOUT"); }
delay(100);

}
``````

This is the video of the working time of flight sensor using an Arduino:

### Putting everything together¶

Now that I understood how each individual component worked and how to use them, I had to combine all of these. I had to connect the SDA and the SCL of the sensor board to the SDA and SCL of the ATtiny1614, respectively. Then I had to upload the same codes as before to read values from the sensor.

## Designing the schematic¶

The libraries I needed to make the schematic are the ATtiny1614 library, fab_new library, and the Adafruit library. I imported these libraries and added them to the design the same way as I did during Electronics Design week.

Making the schematic was actually really easy. I just needed to have the ATtiny1614, 4 headers (for Vcc, ground, Tx, and UPDI), 4 pins for the sensor breakout board, a 4.7K Ω resistor for UPDI programming, an LED for testing, and a 330 Ω resistor for that LED. I then matched all the pins according the the pinout at the very top of this page.

## Designing the board¶

As described on my Electronics Design page, the first thing I have to do is click “switch to .brd” to make the schematic a .brd file. This will produce something like the first image shown below. Then, I had to move all the components to the middle of the board and start rearranging and reorienting them for maximum efficiency. After that, I have to actually start manually drawing the traces with a trace width of 16

## Milling¶

The first time I tried milling this board, I had to add a bit profile since that computer did not have all the new bit profiles. I added the settings to “Fab Acad 1/16” as seen on my week 6 page. I must have mistyped the diameter dimension, since almost every single trace ripped and was completely destroyed. The other bit I used was the “15 degree flute taper stub” I added that is also on my week 6 page.

For the second attempt at milling, I decided to use the “15 degree flute taper stub” bit and the default “1/32 in flat end mill” bit. I also set the trace clearance to 20 mm since then I would only have the traces I need on the board.

## Soldering¶

These are all the components that I need for soldering this board.

And this is the final product:

## Testing¶

To program the board and receive data I only need 4 pins in total. These are power (red wire), ground (black wire), UPDI (yellow wire), and Tx (purple wire).

The first thing I had to do was upload a simple blink program to see if the 1614 was actually receiving commands. This is the same code as earlier where the LED is on pin 2:

For some reason, though, I was unable to upload any program to the chip after that. I will explain how I solved this issue in the “troubleshooting” section

## Troubleshooting¶

After running some tests with Dr. Harris, we concluded that the chip was “bricked” and that I had to replace it. To do this, I used the heat gun and attempted to lift off the chip. To protect the sensor that was located directly adjacent to the chip, Dr. Harris held a piece of scrap PCB between the two regions. When we tried this, we did get the LED to blink and we were able to upload the time of flight, but we were not able to receive any values from the sensor. Dr. Harris hypothesized that the sensor just might not be connecting to the pins. He said this might happen since the solder is not reaching all the way from the sensor to the pin. To resolve this, he said I should use the heat gun to remove the sensor and flip the pins. This means having the small “stub” on the top the board and having the black part and the longer end under the board. I could then attach the board to those pins below the board. Simply put, I am flipping the board to the other side of the board to have better connections.

As you can see, I also did put hot glue next to the pins for programming and receiving data from the board. This is because I dropped my board and I wanted to ensure that the traces would stay intact after this incident.

A final thing Dr. Harris suggested was that I use a FTDI chip to receive the data instead of using an Arduino. This eliminated the step of uploading an additional program to decipher the data that is coming into the Arduino. The only thing I will have to do with the FTDI chip connected is to select the COM Port and click “Serial Monitor” in the top right corner of the Arduino IDE

## What I learned:¶

Previously, I had only ever used very simple input devices like a button or a switch, so it was interesting to learn about this new type of sensor. For those other types of sensors, the only pins required are ground, power, and some sort of data pin. However, with this time of flight sensor, I also had to learn about and use pins such as SDA and SCL. SDA means “Serial Data” and SCL means “Serial Clock”. Both of these are part of the I2C protocol which I had never used before.

## Files¶

ATtiny1614 breakout board