Skip to content

11. Input devices

Part of my final project is a solar powered distance sensor to track the water level. I used Eagle to draw the hello.HC-SR04. I used all the parts in the fab.lbr .

^^This is the basic distance example

^^ In this video I added a peizo speaker and mapped the distance in CM value to the PWM output. It is a quick fun way to represent that data and test an output in relationship to an input.

Files

Eagle: Schematic | Board

Making a Distance Board with ATtiny45

As a sidenote when I was making the board and I clicked on the wrong link and opened the motion board . Below I show how I fixed it and re-milled the board.

After seaching through the library and adding all the correct parts to the schematic I connected the pins to match Neil’s board. The only difference is that I used through-hole pads for the connector to the sensor. I plan to solder wires to the pads to the allow the sensor to be detached from the board.

Once the schematic was arranged I switched to the Board view and arranged all the parts.

I did all this and milled the board before I realized I was designing based on the wrong board. Below you can see the where I made the mistake. The distance sensor needs power, ground and two pins (a total of 4 pins). The motion sensor only needs three pins, power, ground and trigger.

Although I was a bit disappointed, I was glad that I could also switch the 6 FTDI connector from surface mount pads to through-hole pads. On a previous board I made the FTDI pins broke off after a few uses. I think the through-hole will help with durability.

Once the board was fixed with a 4 pin header for the distance sensor I used CopperCAM to mill the board with the Roland Mill. I explain the process in week 5.

Programming the Sensor Board with FabISP

Once I made the board I set all the fuses on the ATtiny45 chip using the process I explained in week 9

Since the FabISP made in week 5 was used to program the ATtiny45 chip and upload the code I connected the 6 pin ISP header from the sensor board to the FABisp for programming.

Once the board was programmed I had to use the FTDI connector to read the values coming from the board. In the schematic you can see that the FDTI supplies power to the board and has one RX pin for sending data to the computer.

Serial Test Code

The first code I uploaded was to test the board. The purpose of this code is to send a quick message over serial to see the board work. When I plugged in the FTDI cable and selected the COM port of the FTDI cable I could see the message in the Serial Monitor.

#include <SoftwareSerial.h>
#define RX  2  // Pin 2 Define RX pin
#define TX  1   // Pin 1 Not using this pin but left because the library looks for it.

SoftwareSerial Serial(RX, TX);

void setup() {

 Serial.begin(9600); //Set communication speed

}

void loop() {


   Serial.println("on..."); //Send a line of text to serial monitor to test
}

Distance Code w/ Serial Feedback

I based my code off the ultra sonic sensor tutorial on random nerd tutorials.

I think it is cool that for the sensor to work you have to know the speed of sound and can calculate the distance by a “simple” equation.

Distance = Speed x Time

Sound travels at 34300 cm per second. In the programing language we use Microsecond so we need to know the speed of sound in micro seconds and can get this by dividing 34300 cm/sec by 1,000. Sound travels .0343 cm/microsecond.

So, if the sensor sends out a signal and it takes 1,000,000 micro seconds to return here is what the math looks like.

34.43 cm = .0343 x 1,000

Since the sound wave is emiting out of the sensor, bouncing off something, and returning the distance is devided by two to only measure the one way trip, not the round trip.

17.215 cm = .0343 x (1,000/2)

#include <SoftwareSerial.h>
#define RX  2 // Pin 2
#define TX  10  // Pin 10 (kept this in because the library looks for it but I am not using the TX pin.)

#define speakerPin 1 //make a pin to send signal to speaker

#define trigPin 3 
#define echoPin 4

long duration, cm, inches;

SoftwareSerial Serial(TX, RX);
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.

   Serial.begin(9600);

   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(speakerPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {


   Serial.println("on...");// wait for a second

digitalWrite(trigPin, LOW);  //this code send voltage down the trigger pin and causes the sensor to emit an ultrasonic wave for 10 microseconds
  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) * .0343;     // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) *.0343;   // Divide by 74 or multiply by 0.0135

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  analogWrite(speakerPin, cm); //I added this code to pulse a peizo speaker creating a sound that reflects the distance measurment once it is converted to CM.
  delay(250);
}

Trouble Shooting Distance Sensor

I uploaded the code and checked the Serial monitor but the measurements never appeared. I knew the code uploaded because the monitor is showing the updated message but the readings were all zero.

I did a visual check and everything seemed soldered perfectly. Finally, I got out a volt meter and checked all the solder points for connection. Turns out the echo pin was not connected at the solder point. I re-soldered the pin on the ATtiny45 chip and tested it again.

Success! It works. I’m excited to the networking week because I am going to figure out how to send the measurement over wifi.