Skip to content

11. Input devices

Assignment

For this week, we were assigned to measure an input device using a microcontroller. I spent some time trying to decide what input I wanted to do (that I could possibly use in my final project) before eventually settling on a hall effect sensor, which measures magnetic fields in close proximity to it.

Group Project

You can find the documentation for the group project here. Angelina and I worked together for most of this, so there was not distribution aside from documentation.

Boards for the Lab/Simplifying the Quentorres

To program this board, I wanted a RP2040 board, but I had lost mine and I was tired of having to make a new one every time it broke or I lost it, and I know other people are as well. Thus, I began to mill multiple Quentorres boards, so that I could solder them and mass produce them for the whole lab. However, I realized many of the components on the Quentorres board are superfluous and it still requires an adapter to program chips like the ATtiny412, so I decided to create my own simplified board which does not require an adapter. I created a design in KiCAD I then milled as many as I could fit on one copper board of both the Quentorres board and my new board. I now had four Quentorres boards and six of my own RP2040 boards. I soldered one of my original boards to test it. These are the six of my own boards I milled:

Sadly, after soldering it and trying to program it, I found out that I had the wrong pins for RX and TX. However, oddly enough, so did the Quentorres board. Nonetheless, I used the Quentorres board schematic to redesign and remill another board. After soldering and testing this board, I found that it worked! This is the KiCAD design and final product for the working simplified programmer board:
I held off before milling more of these, however, because Mr. Dubick said that I should probably send them to a board house to be milled at a much lower cost of time and money, and I needed to work on my actual assignments.

Designing, Milling, and Soldering Board

First, I designed a board to use with a Hall Effect Sensor. I researched the specific sensor we had in the lab with some guidance from Mr. Durrett. I used its data sheet for information on how to begin, and relied on this schematic given in the data sheet for the layout of my designed board.

I created the board using KiCAD, and this is what the original product in KiCAD looked like:

I then milled this board.

I soldered it.

And then I realized that although my board probably worked great, I had no output to display this, nor can an ATtiny412 communicate through serial monitor. I tried to work something out with a bread board, some wires, and some through hole components, but eventually I just decided it would be better if I just created another board. I went back to the schematic and fixed it up to have two output headers. I then milled this board:

Soldered it:


I then hooked up one of the output pins to a servo and tested the board without the hall effect sensor in play, just to ensure that as many of the other components as I could isolate worked. I used this code:

#include <Servo.h>

Servo servo;

void setup() {
  servo.attach(1);  // attaches the servo on pin 3 to the servo object
}

void loop() {
  for (int pos = 0; pos <= 180; pos += 1) { 
    // goes from 0 degrees to 180 degrees in steps of 1 degree
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);                       // waits 15ms for the servo to reach the position
  }
  for (int pos = 180; pos >= 0; pos -= 1) { 
    // goes from 180 degrees to 0 degrees
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);                       // waits 15ms for the servo to reach the position
  }
}

At first, it was not working correctly, but eventually I got it to work.

Trying to Figure out the Hall Effect Sensor

I could not get any signal through I2C however, and Mr. Durrett suggested that I make a board with an on-board LED so that I could more easily have an indicator. For some reason, it took way too many attempts to successfully design, mill, and solder this same board but with a working on-board LED, as I kept making silly errors in the design schematic. This is the eventual conglomerate of all the boards I made (some of which I removed soldered components to reuse them) in trying to get this sensor to work (one is missing here as I threw it across the room in frustration and lost it).

Finally, I created a board with a working on board LED as well as a LED power indicator.

I then went on to try to understand how to program it. I found that Neil Gershenfeld had also used this same sensor with an ATtiny412 microcontroller (which would have been helpful to know before this). Luckily, his board design seemed to match with mine, so it seemed like I was on a good track. I then spent a long duration of time just trying to understand the library, a library used to send and receive messages through I2C. In Dr. Gershenfeld’s code, he used this library to configure the Hall Efffect Sensor and certain registers in it. I tried the code, but could not get any indication of it working. The fact that the ATtiny412 does not work with Serial Monitor makes it even harder to troubleshoot. After messing around with the code and matching the address definition with the address given on the data sheet, I still could not get anything. Eventually, I had to give up on this and just try a different input.

Step Response

My fellow classmate, Connor Cruz, had used a step response for his input, which I thought was interesting and wanted to try myself. A step response basically sends a signal through a 1M Ohm resistory from the TX pin (preferably to flat copper board) and receives a signal from another flat copper board a small distance apart from the first one using the RX pin. These two boards will act as a capacitor for the signal, and the signal will vary based on the dielectric constant (a higher dielectric constant is able to hold more charge) of the matter between these two boards, as this will not allow as much of the original signal to pass through. In very short periods (a few microseconds), The TX pin will send a high signal then no signal, and the RX pin will measure these as compared to the common ‘step response curve’.

I also wanted to incorporate my networking week’s assignment into this, as I was falling behind and thought they could go well together. I began by designing the board which would hold the ATtiny412, the LED and would connect through SCL and SDA to another board. I talk more about the attempt to do I2C between ATtiny412 boards in my Week 13 documentation. I ended up using these boards. BELOW IS SHOWN THE BOARD AFTER I ADDED 4.99k RESISTORS, NOT THE ONE I ENDED UP USING. THE BOARD BELOW WILL LIKELY NOT WORK WITH A STEP RESPONSE. TO MAKE IT WORK, JUST TAKE AWAY THE 4.99k RESISTORS. (I unfortunately did not save the other board and somehow did not screenshot it).

Although I realized that this board (not the one shown above) does not work with I2C after I made two of them because it does not include pullup resistors from SCL and SDA to power, the boards which I later made (the one shown above) which included these would not work because of this. Because I had used the TX and RX pins which happened to also be SCL and SDA, the resistors on the board shown above, which were for I2C,messed with the signal for the step response, so my flawed board actually came in handy.

I then needed to make the actual module with the main part of the step response. Because I was basing this off of Connor’s version, he had named his board the “Flubben I”, so I created the “Flubben II”. When I originally milled this board, the holes did not mill, so when I went to remill some of the other boards because of some errors I made in the schematic for I2C connection, I also decided I was going to remill the Flubben II with holes. Oddly enough, I struggled for a while trying to get holes in the g-code. Even when I would create circles (both filled and unfilled) and set them to the “edge cuts” layer, and it would render holes in the 3D render in KiCAD, it would not mill these. Finally, it suddenly worked. In addition to the Flubben II, I created two small, plain, rectangular boards to attach to a male header which would also attach to the holes on the Flubben II. In this way, the boards would all be connected, as compared to the soldered jumper wire connection in the Flubben I. Finally, when I got this to work, the Flubben II looked like this:

I milled these out and began to solder them:
The holes on the Flubben II were too big for the male headers though, which made it very hard to create a solder bridge to connect them. I got it on one side, but on the other side, the solder was so reluctant to connect these two that it both started melting the plastic part of the male header and connecting the male header to the ‘background’ of the board, where it clustered up. I eventually just cut a small piece of wire from a through hole component and used it to link the trace to the ‘background’ of the board. Because it had also slightly clustered on the other side on the ‘background’, I found the thinest connection between the two and cut it away with a knife. Although it was very scuffed, I could now program my step response. Below is my first mill and the final one.

Programming

I now had to program the step response. As I explained previously, it uses a fairly simple process of sending a signal and measuring it on the other end, but it takes thousands of these samples a second. I used Connor Cruz’s code to program my step response, but I added in comments on every line to display my understanding of what the program does. Unfortunately, I had added my on-board LED to the only pin on the ATtiny412 which does not do analog, so I could only use it as a digital pin. Because of this, I took away the part of the code which matches the LED’s brightness with the mapped analog value, although I had commented this code as well before I realized this. This is the commented code that I used:


#define rxpin 3 // RX pin on ATtiny412
#define txpin 2 // TX pin on ATtiny412
#define settle 100 // Time for voltage to settle
#define samples 100 // How many samples of analogRead it takes in given time

void setup() {
  pinMode(txpin, OUTPUT); // Initializing the TX pin as OUTPUT (for sending signals through TX and recieving and measuring those same signals and their strengths through RX)
  pinMode(0, OUTPUT); // Initializing LED pin for indication
}

void loop() { // Main function looping through the other various functions

  //Uncomment one function depending on which test you want to run.
   instantCheck();
   //analogLED();
}

long int average() { // Function to find the average change in signal
  long int up, down, avg; 
   avg = 0; // Initializing avg as 0
   noInterrupts(); // Disables all other activities
   for (int i = 0; i < samples; i++) { // For loop which loops (samples) times
      digitalWrite(txpin, HIGH); // Sends signal through TX pin
      up = analogRead(rxpin); // Recieves signal through RX pin
      delayMicroseconds(settle); // Delays for 100 microseconds so that signal can level out
      digitalWrite(txpin, LOW); // Takes away signal from TX
      down = analogRead(rxpin); // Reads RX pin after removing signal
      delayMicroseconds(settle); // Delays again so that signal can level out
      avg += up - down; // Measuring capacitance of system as in theory avg should be 0 if copper plates were connected
   }
   interrupts(); // Allows other activites to occur again
   return avg; // Returns the avg value
}

void instantCheck() { // Function to simply check functionality of the program
  long int avg = average(); // Takes one sample of average() function
  if (avg > 3000) { // Mapped average for my sized boards
    digitalWrite(0, LOW); 
    digitalWrite(rxpin, HIGH); // Turns LED on if avg is greater than 9000 (exterior influence was high)
  }
  else {
    digitalWrite(0, HIGH); 
    digitalWrite(rxpin, LOW);
  }
}

I connected my programmer to the ATtiny412 I2C board, which was connected through male headers to the Flubben II. After a little bit of trial and error with the conditional for the ‘avg’ value, I got it to work!

Week Reflection

This week took way more time than it should have, but I certainly learned some about different types of inputs and how they are read by a microcontroller. I still have not figured out I2C despite my setups loooking much like those of people who have gotten it to work. However, step response has piqued my interest, and I will likely incorporate that into my final project.

File Downloads

To download my files used in this week’s processes, use this link.


Last update: May 7, 2024