11. Input Devices¶
Learning outcomes - Demonstrate workflows used in circuit board design and fabrication - Implement and interpret programming protocols
Have you? - Described your design and fabrication process using words/images/screenshots or linked to previous examples. - Explained the programming process/es you used and how the microcontroller datasheet helped you. - Explained problems and how you fixed them - Included original design files and code
Background¶
Time to pivot. Let’s figure something else out. I have stuff designed with a thermistor and thermocouple, but the amplification of the thermocouple is like a lot with trying to figure out the ~500 multiplier needed leads to a bunch of weird feeling calculations. So instead, let’s do the thing instead with the distance sensor! I used this in conjunction with a motor, but I’ll talk about that later because it’s a different assignment and I need to get credit for that assignment too.
So anyways. Here we go!
So we have some stuff with a distance sensor. It technically uses an input AND AN OUTPUT!! SHOULD TOTALLY COUNT FOR BOTH, AMIRITE?! Please?
Anyways, let’s look at it. So here we go!
So look at the thing above from the link. It sends out a signal sonically, then hears it back and figures out the time between sending out the signal and receiving it back. This works because sound bounces off of stuff - review basic physics for more info. Since we know the speed of sound, we can figure out how far something away based on the time to receive stuff since that is how solving equations works. If you look at the datasheet for the thing, it needs power, a ground, and 2 digital pins - one for the trigger and one for the echo. Cool. Inspired by the design inspired by the lecture, I adjusted it to use an ATtiny44 instead so we could add other stuff like motors or buttons or cappuccino machines.
Change the pins in a program from a guide on here, etc, etc, and let’s check it out! Maybe throw in an LED or something? It would be cool to do an RGB LED that can change the color based on the range - maybe later. For now, sensor and stuff.
Board¶
Use Eagle, like all the time in this previous stuff. Use the example from Neil, check the pins on that one really cool picture I said I would use a lot, and then do the thing with the stuff to make the board. Milled it with an MDX-15 using super old mods, etc etc. Solder, blah blah blah. Kinda routine at this point.
Pictures of stuff made:
Programming¶
Remember that tutorial there was a link to like right up there? Followed that. Also found another slightly different one that I did use a bunch of stuff in. I also took the basic h-bridge output code from Neil’s example to incorporate that.
Summary¶
The trickiest part of working with this is getting the sensor to not constantly trigger to turn on the motor. A nice housing for it would help, as would a switch for it. The programming was pretty straightforward as usual. I also worked on another input, a thermistor, which can be seen on networking week.
Code here:¶
Testing the distance sensor:
#include <SoftwareSerial.h> // *** // *** Define the RX and TX pins. Choose any two // *** pins that are unused. Try to avoid D0 (pin 5) // *** and D2 (pin 7) if you plan to use I2C. // *** #define RX 4 // *** D3, Pin 2 #define TX 4 // *** D4, Pin 3 // *** // *** Define the software based serial port. Using the // *** name Serial so that code can be used on other // *** platforms that support hardware based serial. On // *** chips that support the hardware serial, just // *** comment this line. // *** SoftwareSerial Serial(RX, TX); /* * created by Rui Santos, https://randomnerdtutorials.com * * Complete Guide for Ultrasonic Sensor HC-SR04 * Ultrasonic sensor Pins: VCC: +5VDC Trig : Trigger (INPUT) - Pin11 Echo: Echo (OUTPUT) - Pin 12 GND: GND */ #define trigPin 10 // Trigger #define echoPin 9 // Echo long duration, cm, inches; void setup() { //Serial Port begin Serial.begin (9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); // Convert the time into a distance cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343 inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135 Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(250); }
Code for the triggering the motor:
int ledpin = 7; int in1 = 3; int in2 = 2; int trig = 10; int echo = 9; int test = 0; long duration; int distance; void setup() { // put your setup code here, to run once: pinMode(ledpin, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(trig, OUTPUT); pinMode(echo, INPUT); analogWrite(in1, 0); analogWrite(in2, 0); digitalWrite(ledpin, HIGH); } void loop() { // put your main code here, to run repeatedly: while (test = 0) { digitalWrite(ledpin, HIGH); delay(10000); test = 1; } digitalWrite(ledpin, LOW); analogWrite(in1, 0); analogWrite(in2, 0); digitalWrite(trig, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trig, HIGH); delayMicroseconds(10); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echo, HIGH); digitalWrite(trig, LOW); // Calculating the distance distance= duration*0.034/2; if (distance < 10) { digitalWrite(ledpin,HIGH); analogWrite(in1, 0); analogWrite(in2, 240); delay(30000); analogWrite(in2, 0); } }
Also included was an output to a motor if the distance was less than 5 cm. Video of it working! I did measure stuff and it was accurate, but I didn’t take pictures of it because I took notes of it working instead because that was way more convenient because cameras take time to pull out and I ain’t got now time for that.
Video¶
Please note that this was a slightly different board than the one shown, as I added in some stuff for switches to the schematics shown above. I didn’t get a picture of the actual board used below, since I cannibalized it for parts.
Dog!¶
Files¶
3D Files can be seen in machine design files.
Schematic
Board