Skip to content

12. Output 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. Outlined problems and how you fixed them Included original design files and code

Background

Output devices! If you check out my week 11, it has evidence of a working output device, in this case a motor! So yay!

The programming was pretty straightforward, but I did have a couple of issues. There was one point where no matter what, the h-bridge was putting out a voltage signal to both of the outputs, despite only one of the “ins” receiving a signal. To overcome this and have it run (this specific conveyor belt setup only required one direction of travel), we snipped the line on one of the h-bridge outputs to the motor and connected it to ground. And it worked!

Code and other board files are in the previous week, also shown here:

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. Initially on the board, there was going to be a potentiometer, but the footprint I had did not match anything in stock, and the board was already milled. So I used an exacto knife, cut some parts, and put in an LED to light up when something was a certain distance away to make sure the code was correct. 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 // // 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); }


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.

Files

But here are the links to the files for the conveyor belt parts:

conveyor belt and sprocket

body

board