I still struggling to find functional code that works for my project. In the meantime, I was studying the PWM function with Arduino again.
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.
In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
I also learned how to combine together the code for temperature sensor and the code for PWM. For example:
Code for temperature sensor:
#include <LiquidCrystal.h>
float tempC;
float tempF;
float voltage;
int reading;
int tempPin=0; //analog channel
int interval = 50; //in ms (how often screen reprints
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
// set up the LCD’s number of rows and columns:
lcd.begin(20,4);
// Print a message to the LCD.
lcd.setCursor(0,1);
lcd.print("Cabinet Temperature:");
}
void loop() {
//TMP36 voltage at the output varies linearly with ambient temperature.
//As a result,it can be used as a thermometer according to the equation:
// ᵒC = (Vout(V) - .5)*100
//To find Vout in V, we use the following equation for a 5V input
// Vout(V) = AnalogReading*(5/1024)
reading = analogRead(tempPin); //read the value from the sensor
voltage = reading*(5.0/1024); //convert reading to voltage (in V), for 5V input
tempC = (voltage-0.5)*100; //convert voltage to temperature
tempF = ((tempC*9/5)+32); //convert C temperature to F
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 2);// set the cursor to column 0, line 1
lcd.print(tempF); //Print Fahrenheit temperature to LCD
lcd.print((char)223); // degree symbol
lcd.print("F ");
lcd.setCursor(0,3); //print set temp to lcd
lcd.print("Set Temp: 80 ");
lcd.print((char)223);
lcd.print("F");
}
Code for PWM fan:
// Define which pin to be used to communicate with Base pin of TIP120 transistor
int TIP120pin = 3; //for this project, I pick Arduino's PMW pin 3
void setup()
{
pinMode(TIP120pin, OUTPUT); // Set pin for output to control TIP120 Base pin
analogWrite(TIP120pin, 255); // By changing values from 0 to 255 you can control motor speed
}
void loop()
{
}
Code for combined circuit:
#include <LiquidCrystal.h>
float tempC;
float tempF;
float voltage;
int reading;
const int tempPin = 0; //analog channel
const int TIP120pin = 3; //for this project, I pick Arduino's PMW pin 3
const int interval = 50; //in ms (how often screen reprints
const int setPointF = 80;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
// set up the LCD’s number of rows and columns:
lcd.begin(20,4);
// Print a message to the LCD.
lcd.setCursor(0,1);
lcd.print("Cabinet Temperature:");
pinMode(TIP120pin, OUTPUT); // Set pin for output to control TIP120 Base pin
}
void loop() {
//TMP36 voltage at the output varies linearly with ambient temperature.
//As a result,it can be used as a thermometer according to the equation:
// ᵒC = (Vout(V) - .5)*100
//To find Vout in V, we use the following equation for a 5V input
// Vout(V) = AnalogReading*(5/1024)
reading = analogRead(tempPin); //read the value from the sensor
voltage = reading*(5.0/1024); //convert reading to voltage (in V), for 5V input
tempC = (voltage-0.5)*100; //convert voltage to temperature
tempF = ((tempC*9/5)+32); //convert C temperature to F
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 2);// set the cursor to column 0, line 1
lcd.print(tempF); //Print Fahrenheit temperature to LCD
lcd.print((char)223); // degree symbol
lcd.print("F ");
lcd.setCursor(0,3); //print set temp to lcd
lcd.print("Set Temp: 80 ");
lcd.print((char)223);
lcd.print("F");
// If the temperature is highher than the set point, run the fans.
// Fans reach full speed then temperature is more than 10°F above setpoint.
analogWrite(TIP120pin, constrain( (tempF - setPointF) * 25, 0, 255));
}
With this examples I will try to come up with new code that can work for my project.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment