Idea:
This week our task was to take digital and analog inputs, and light up LED lights with both digital and analog outputs. My goal was to make something similar to the thermostat on your wall. I wanted a power button, and a dial where you could turn up or down the temperature. I would then light up LED’s to show this working as if the heater or AC was turning on.
Implementation:
Digital Input: I started off using the button setup that we tried in class but decided it would be nicer if I used a switch. I found that a switch was wired very similarly to the button but was always in a 0 or 1 state. This made the code very easy as it did not require and saving of the previous state.
Analog Input: I needed two inputs here, the target temperature that the user wants which utilized a dial in the form of the potentiometer, as well as the room temperature which I tried using the included thermometer to implement. I found the wiring schematics online (short circuited it once by setting it up backwards), and managed to get analog outputs from it. The tricky part was converting from the analog input to degrees. I found that you could convert the analog input value into volts. The thermometer worked at a 10mV per degree linear ratio so I was able to convert it quite easily with the preset offset of 500mV from there. I checked the Serial.write value with my room thermostat and both matched.
Digital Output: The most basic part of my circuit was to light up a green power button if the system was on. I just wrote to a green LED high when the switch was on and low when it was off.
Analog Output: To light up the appropriate LED’s to represent the heater or AC turning on, I had to first get a target temperature from the potentiometer and compare that to the room temperature. If the room was warmer than the target I would light up the blue LED as if AC was turning on, and the red if the opposite was true to simulate the heater. I also lit up the LED’s stronger when the room temperature was further from the target temperature.
Results:
Here is a demo where I adjust the potentiometer to change the target temperature, as well as turn the system on and off with the switch.
Here is an example where I apply heat to the thermometer. This causes the system to think that the room is warm and you can see the AC light turn on. As it gets warmer the AC light gets brighter. (Headphone users beware)
Circuit:
Code:
//Pins const int tempPin = A0; const int knobPin = A1; const int switchPin = 2; const int green = 3; const int red = 5; const int blue = 6; bool on = false; float temp; //What is the current temp from thermometer reading float target; //Based on potentiometer what is the desired temperature void setup() { //Start console comunication Serial.begin(9600); //Set pin modes pinMode(green, OUTPUT); pinMode(red, OUTPUT); pinMode(blue, OUTPUT); pinMode(switchPin, INPUT); //Set on off initial value on = digitalRead(switchPin); } void loop() { //Read in temperature value and convert it to celcius from analog in temp = (((analogRead(tempPin) * 5.0) / 1024) - 0.5) * 100; //Convert from reading to mV to celcius with a 10mv per degree rating on = digitalRead(switchPin); //If on turn on lights if (on) { //Turn power (green light) on digitalWrite(green, HIGH); //Get the target temp from the potentiometer should be a value between 15 and 27, 21 is standard in the middle value target = map(analogRead(knobPin), 0, 1023, 15.0, 27.0); float difference = abs(map(temp - target, 0, 7, 0, 100)); //How far off the target value is it?, mapped to a value from 0 to 100 for an led to take //If hotter turn on red light up to 100 (so dimness is more clear) Serial.println(difference); if (temp - target < 0) { analogWrite(red, difference); analogWrite(blue, 0); } //Cooler than target else if (temp - target > 0) { analogWrite(red, 0); analogWrite(blue, difference); } //By some chance equal else { analogWrite(red, 0); analogWrite(blue, 0); } } else { //Turn power (green light) off digitalWrite(green, LOW); digitalWrite(red, LOW); digitalWrite(blue, LOW); } // Serial.println(" "); // Serial.print("Temp: "); // Serial.println(temp); // Serial.print(" Knob: "); // Serial.print(analogRead(knobPin)); }