Liz McCormick

HOW TO MAKE [ALMOST] ANYTHING_MAS.863

Week 10 - Output Devices

 


Project 02b

This weeks assignment was to add an output device to a microcontroller board you've designed and program it to do something. Working towards my final project, I used a relay to regulate a storebought heater and humidifier.
Project 02a
The final board that I made (and used in the final project) was the fabkit board (with an added resonator). Given the scope of the project and the amount of time I had left, this was really my only option.
Project 02c
The fabkit board is an arduino-friendly board with multiple empty pins. Since I wasn't entirely sure how my electronics would be working, I appreciated the flexibility that this board provided. The above images come from the author of the fabkit tutorial, they are not my images.
I had quite a bit of struggles with the board - first of all, I wasn't able to mill the holes, which I didn't think would be a problem, but then I ended up using a lot of the empty pins, so I had to work out some solder magic.
Using the directions from the fabkit site, I couldn't get the bootloader to work. In fact, I couldn't even get the library to load. No one in my section could figure this out, so the problem was never resolved. I was finally able to burn bootloader after I installed a 16 mhz resonator. 20 did not work, which is what I used on my previous board. Make sure you have your speed set right. See the image after the code below. (Burning the bootloader means that you can work with your board without using your programmer. This alleviates a bit of wiring connections and just generally simplifies things. It's still possible to program your board without burning the bootloader, though).
Using the DHT-22 arduino library file, I was able to get the sensor to read temperature and humidity. The only thing I had to change was the pin number (and since arduino uses different pin names then the Atmega data sheet, I had to look up the conversion process. This is outlined in the "input devices" week). Here's what the code looked like for that test:

				#include 

				#define DHTPIN 2     // what digital pin we're connected to

				// Uncomment whatever type you're using!
				//#define DHTTYPE DHT11   // DHT 11
				#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
				//#define DHTTYPE DHT21   // DHT 21 (AM2301)

				// Connect pin 1 (on the left) of the sensor to +5V
				// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
				// to 3.3V instead of 5V!
				// Connect pin 2 of the sensor to whatever your DHTPIN is
				// Connect pin 4 (on the right) of the sensor to GROUND
				// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

				// Initialize DHT sensor.
				// Note that older versions of this library took an optional third parameter to
				// tweak the timings for faster processors.  This parameter is no longer needed
				// as the current DHT reading algorithm adjusts itself to work on faster procs.
				DHT dht(DHTPIN, DHTTYPE);

				void setup() {
				  Serial.begin(9600);
				  Serial.println("DHTxx test!");

				  dht.begin();
				}

				void loop() {
				  // Wait a few seconds between measurements.
				  delay(2000);

				  // Reading temperature or humidity takes about 250 milliseconds!
				  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
				  float h = dht.readHumidity();
				  // Read temperature as Celsius (the default)
				  float t = dht.readTemperature();
				  // Read temperature as Fahrenheit (isFahrenheit = true)
				  float f = dht.readTemperature(true);

				  // Check if any reads failed and exit early (to try again).
				  if (isnan(h) || isnan(t) || isnan(f)) {
				    Serial.println("Failed to read from DHT sensor!");
				    return;
				  }

				  // Compute heat index in Fahrenheit (the default)
				  float hif = dht.computeHeatIndex(f, h);
				  // Compute heat index in Celsius (isFahreheit = false)
				  float hic = dht.computeHeatIndex(t, h, false);

				  Serial.print("Humidity: ");
				  Serial.print(h);
				  Serial.print(" %\t");
				  Serial.print("Temperature: ");
				  Serial.print(t);
				  Serial.print(" *C ");
				  Serial.print(f);
				  Serial.print(" *F\t");
				  Serial.print("Heat index: ");
				  Serial.print(hic);
				  Serial.print(" *C ");
				  Serial.print(hif);
				  Serial.println(" *F");
				
So once I ran that code... I didn't get any errors... but where did the data go? Do I need to build an interface to read it? Fortunately arduino has a built in reader, just go to Tools>Serial Monitor. Good stuff. Here's what that looked like:
Project 02b
Temperature and Humidity readings. I literally cried with happiness when I got this working. What a cool feeling. Thank goodness for Hunmin magic. Now onto the output.
Project 02b
With the recommendation of my colleagues, I bought the "SunFounder 2 Channel DC 5V Relay Module with Optocoupler Low Level Trigger Expansion Board for Arduino UNO R3 MEGA 2560 1280 DSP ARM PIC AVR STM32 Raspberry Pi" to go with my fabkit board.
Using this device was somewhat intimidating since I'm actually rerouting the power supply to various devices. The first step is to cut only ONE of the two wires, stripping it, and stuffing it into the box. I never knew that power cords had two different types of wires - one is grooved/striped/ribbed and the other is smooth. The grooved wire is grounded (neutral) while the smooth wire is ungrounded (hot). The smooth (hot) wire connects to the relay, so cut that one.
Project 02c
I used some code that I found online to test the relay. It turns it on and off - you can tell it's working if you hear a click and the red light flashes. Very cool. Before plugging it into the power source, we made sure that the pins were active and the code was working, using a micrometer.
I should add that there is a bit of a shock risk with this thing, and Hunmin recommended that I put something over the back of the board since the pins go through. Also when you're stuffing the wires in, make sure that all the exposed wire is in there - nothing should be hanging out. This may mean that you have to cut the wire pretty short. Also, if the wires are super thin (life they were for my heater), you may have to add some Solder onto it so it stays in the pins.
Project 02c
Once I got the heater (USB) and the humidifer (power) wires stuffed in there, everything went pretty smoothly after that. I even figured out the code all by myself! (with some help from the internet of course). I've learned that coding is just copying bits and pieces from other people and changing pins and inputs/outputs to match what you're doing. I'm still totally intimidated by the process, but not nearly as much as I was before. Here's the final code that I used:



				#include 

				#define DHTPIN 7     // what digital pin we're connected to
				#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
				#define humid A4
				#define heater A5

				float maxHum = 70.0;
				float maxTemp = 30.0;

				DHT dht(DHTPIN, DHTTYPE);

				void setup() {
				      pinMode(heater, OUTPUT);
				      pinMode(humid, OUTPUT);

				      digitalWrite(heater, HIGH);
				      digitalWrite(humid, HIGH);

				  Serial.begin(9600);
				  Serial.println("DHTxx test!");
				  dht.begin();
				}

				void loop() {
				  // Wait a few seconds between measurements.
				  delay(2000);

				  // Reading temperature or humidity takes about 250 milliseconds!
				  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
				  float h = dht.readHumidity();
				  // Read temperature as Celsius (the default)
				  float t = dht.readTemperature(true);
				  // Read temperature as Fahrenheit (isFahrenheit = true)
				  float f = dht.readTemperature(false);

				  // Check if any reads failed and exit early (to try again).
				  if (isnan(h) || isnan(t) || isnan(f)) {
				    Serial.println("Failed to read from DHT sensor!");
				    return;
				  }


				  if(t > maxTemp) {
				      digitalWrite(heater, LOW);
				  } else {
				     digitalWrite(heater, HIGH);
				  }

				  if(h > maxHum) {
				      digitalWrite(humid, LOW);
				  } else {
				     digitalWrite(humid, HIGH);
				  }


				  Serial.print("Humidity: ");
				  Serial.print(h);
				  Serial.print(" %\t");
				  Serial.print("Temperature: ");
				  Serial.print(t);
				  Serial.println(" *F");
				}
			
Project 02c
It was certainly steamy in there!
Project 02c
I had an accidental leak at the front of the box, so it spewed steam out the front. It's OK though, because it actually looked pretty cool and attracted a lot of attention.
Project 02c
Steam spewing out the front.