Temperature-Controlled Fan Using Arduino

Temperature-Controlled Fan Using Arduino

The objective of this project is to build a temperature-controlled fan using Arduino. The fan’s speed will be automatically adjusted based on temperature readings from a temperature sensor. By using PWM (Pulse Width Modulation), the fan speed will increase or decrease depending on the temperature. This project covers how to use analog input, PWM, and if-else conditions to implement an efficient Arduino fan control system.

Fundamental Programming Concepts

  • Control Structures (if-else): Used to decide whether the fan should be turned on, off, or adjusted based on temperature.
  • Analog Input: Reads the value from the temperature sensor through the analogRead() function.
  • PWM (Pulse Width Modulation): Controls the fan’s speed by adjusting the power using analogWrite().
  • Digital Output: Sends signals to control whether the fan is on or off using digitalWrite().

Requirement Components

To complete this temperature-controlled fan Arduino project, you will need:

  • Arduino Uno Board
  • Temperature Sensor (LM35 or similar)
  • DC Fan (PWM-capable)
  • NPN Transistor (e.g., 2N2222) (to control the fan)
  • Diode (to prevent back EMF from the fan)
  • 10kΩ Resistor
  • Breadboard
  • Jumper Wires
  • USB Cable (for connecting the Arduino to your computer)

Circuit Diagram

Circuit Connection

Component Arduino Pin
Temperature Sensor (Vcc) 5V
Temperature Sensor (GND) GND
Temperature Sensor (Output) A0 (Analog Input)
Fan (Positive) Transistor Collector
Fan (Negative) GND
NPN Transistor Base Pin 9 (PWM Output)
Diode Between fan and power supply

How to Connect the Circuit

  1. Connect the temperature sensor to the A0 pin for analog input. The other pins go to 5V and GND.
  2. Connect the fan between the collector of the NPN transistor and GND.
  3. The base of the NPN transistor should be connected to Pin 9 (PWM-capable pin) on the Arduino through a 10kΩ resistor.
  4. Place a diode across the fan to prevent back EMF from damaging the transistor.

Explanation of Circuit

  • The temperature sensor reads the ambient temperature and outputs a voltage that corresponds to the current temperature.
  • The Arduino reads the sensor’s value using analog input (A0) and adjusts the fan’s speed based on the temperature. This is done through PWM signals.
  • The transistor acts as a switch to control the fan, while the diode protects against voltage spikes when the fan turns off.

Programming Section for Temperature-Controlled Fan Using Arduino

Arduino Syntax

Topic Name Syntax Explanation
Analog Read analogRead(pin) Reads analog value from the specified pin (0-1023).
Analog Write (PWM) analogWrite(pin, value) Outputs a PWM signal (0-255) to control fan speed.
Digital Write digitalWrite(pin, value) Sets a digital pin to HIGH or LOW.
If-else Condition if (condition) { } else { } Executes code based on whether a condition is true or false.

Arduino Code:

Here’s the Arduino code to control the fan based on temperature readings:

// Global variables
const int tempSensorPin = A0;  // Pin connected to the temperature sensor
const int fanPin = 9;          // PWM-capable pin connected to the transistor
int tempValue = 0;             // Variable to store the temperature reading
int fanSpeed = 0;              // Variable to store the fan speed
void setup() {
  pinMode(fanPin, OUTPUT);     // Set the fan pin as output
  Serial.begin(9600);          // Initialize serial communication for debugging
}
void loop() {
  // Read temperature sensor value (0-1023)
  tempValue = analogRead(tempSensorPin);
  // Convert the analog value to Celsius (assuming LM35 sensor)
  float voltage = tempValue * (5.0 / 1023.0);
  float temperatureC = voltage * 100.0;
  // Print temperature value to the Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.println(" °C");
  // Control fan speed based on temperature thresholds
  if (temperatureC < 25) {
    fanSpeed = 0;                // Turn off the fan if temperature is below 25°C
  } else if (temperatureC >= 25 && temperatureC < 30) {
    fanSpeed = 128;              // Set fan to half speed for temperatures between 25°C and 30°C
  } else if (temperatureC >= 30) {
    fanSpeed = 255;              // Set fan to full speed if temperature exceeds 30°C
  }
  // Set the fan speed using PWM
  analogWrite(fanPin, fanSpeed);
  delay(1000);  // Wait 1 second before reading temperature again
}

Steps to Upload Code:

  1. Connect your Arduino to your computer using the USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino.
  5. Open the Serial Monitor to view the temperature readings and see how the fan responds to changes in temperature.

Check Output

Once the code is uploaded, the fan should turn on or off based on the temperature:

  • Below 25°C: The fan should remain off.
  • Between 25°C and 30°C: The fan should run at half speed.
  • Above 30°C: The fan should run at full speed.

The temperature values will be printed in the Serial Monitor.

Troubleshooting Tips

  • Fan not turning on? Double-check the transistor connections and ensure the base is connected to a PWM-capable pin.
  • Temperature not reading correctly? Ensure the temperature sensor is connected properly, and verify the sensor’s output range.
  • No output on Serial Monitor? Verify that the baud rate in the Serial Monitor is set to 9600 and that the correct COM port is selected.

Further Exploration

  • More Temperature Ranges: Add more temperature thresholds to create a finer control over the fan speed (e.g., low, medium, high).
  • LCD Display: Display the current temperature and fan speed on an LCD instead of the Serial Monitor.
  • Control Multiple Fans: Expand the project to control multiple fans at different speeds based on varying temperature ranges.

Note

This project introduces key concepts such as analog input, PWM control, and if-else conditions in Arduino programming. These concepts are essential for building more complex Arduino-based control systems like temperature-controlled devices.

FAQ

Q1: What does analogWrite() do in Arduino?
The analogWrite() function outputs a PWM signal that simulates an analog signal, allowing you to control devices like motors or fans with varying speeds.

Q2: How does the temperature sensor work?
The temperature sensor outputs a voltage that corresponds to the ambient temperature. This value is read by the Arduino using analogRead() and converted into degrees Celsius.

Q3: What is PWM, and why is it used for controlling fan speed?
PWM (Pulse Width Modulation) allows you to control the speed of a fan by varying the amount of time the signal is on versus off. This gives more control over devices that don’t support direct analog input.

Q4: Can I add more temperature ranges for finer control of the fan speed?
Yes, you can add more if-else conditions to create additional fan speed levels based on different temperature ranges.

Q5: Can I use a relay instead of a transistor to control the fan?
Yes, you can use a relay for switching the fan on or off, but you won’t be able to control the speed unless you use a transistor or a dedicated PWM controller.

Basic calculator using Arduino with Buttons

Basic calculator using Arduino with Buttons

The objective of this project is to create a basic calculator using Arduino and buttons for input. The calculator will perform arithmetic operations like addition, subtraction, multiplication, and division based on user input. This project introduces the use of arithmetic operators, if-else conditions, and switch case structures in Arduino programming.

Fundamental Programming Concepts

  • Arithmetic Operators: Use operators such as +, , *, and / to perform mathematical calculations.
  • if-else Conditions: Control the logic to perform different actions based on button presses.
  • Digital Inputs: Use buttons to input numbers and operations into the calculator.
  • Switch Case: Select the arithmetic operation (addition, subtraction, multiplication, or division) based on user input.

Requirement Components

To build this simple Arduino calculator, you’ll need the following components:

  • Arduino Uno Board
  • Push Buttons (at least 6) (to input numbers and operations)
  • 10kΩ Resistors (for pull-down resistors)
  • Breadboard
  • Jumper Wires
  • USB Cable (to connect Arduino to your computer)

Circuit Diagram

Circuit Connection

Component Arduino Pin
Button 1 (Number 1) Pin 2
Button 2 (Number 2) Pin 3
Button 3 (Addition) Pin 4
Button 4 (Subtraction) Pin 5
Button 5 (Multiplication) Pin 6
Button 6 (Division) Pin 7
Ground (for all buttons) GND

How to Connect the Circuit

  1. Insert each button into the breadboard.
  2. Connect one side of each button to the specified Arduino pin.
  3. Connect the other side of each button to GND using a 10kΩ pull-down resistor.
  4. Ensure that the circuit is connected correctly before proceeding.

Explanation of Circuit

  • Each button serves as an input for numbers and arithmetic operations. When pressed, the buttons send a HIGH signal to the corresponding Arduino pin.
  • The Arduino uses digitalRead() to check the button presses and stores the numbers and operation in variables. The switch case structure handles the arithmetic operation based on user input.

Programming Section

Arduino Syntax

Topic Name Syntax Explanation
Digital Read digitalRead(pin) Reads the state of a digital input pin (HIGH or LOW).
If-else Condition if (condition) { } else { } Executes code based on a condition being true or false.
Switch Case switch (variable) { case … } Executes a block of code based on the value of a variable.
Arithmetic Operators +, , *, / Used for performing basic mathematical operations.

Arduino Code for basic calculator using Arduino

Here is the Arduino code to implement the simple calculator with buttons:

// Define pins for buttons

const int button1 = 2;
const int button2 = 3;
const int addButton = 4;
const int subButton = 5;
const int mulButton = 6;
const int divButton = 7;
// Variables to store input values and result
int number1 = 0;
int number2 = 0;
char operation;
int result = 0;
void setup() {
  // Set button pins as input
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(addButton, INPUT);
  pinMode(subButton, INPUT);
  pinMode(mulButton, INPUT);
  pinMode(divButton, INPUT);
  // Start Serial Communication
  Serial.begin(9600);
}
void loop() {
  // Check if number buttons are pressed
  if (digitalRead(button1) == HIGH) {
    number1 = 1;  // Number 1
  }
  if (digitalRead(button2) == HIGH) {
    number2 = 2;  // Number 2
  }
  // Check if operation buttons are pressed
  if (digitalRead(addButton) == HIGH) {
    operation = '+';
  }
  if (digitalRead(subButton) == HIGH) {
    operation = '-';
  }
  if (digitalRead(mulButton) == HIGH) {
    operation = '*';
  }
  if (digitalRead(divButton) == HIGH) {
    operation = '/';
  }
  // Perform calculation using switch case
  switch (operation) {
    case '+':
      result = number1 + number2;
      break;
    case '-':
      result = number1 - number2;
      break;
    case '*':
      result = number1 * number2;
      break;
    case '/':
      if (number2 != 0) {
        result = number1 / number2;
      } else {
        Serial.println("Error: Division by zero");
      }
      break;
  }
  // Print the result to the Serial Monitor
  Serial.print("Result: ");
  Serial.println(result);
  delay(1000);  // Add a small delay before next input
}

Steps to Upload Code:

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino.
  5. Open the Serial Monitor to view the result of your calculations.

Check Output

After uploading the code, press the buttons corresponding to numbers and operations. You should see the result of the operation displayed on the Serial Monitor. For example, pressing the button for 1, followed by the addition button, then pressing 2, will give the result 3.

Troubleshooting Tips

  • Button not registering input? Ensure the buttons are properly connected to their respective pins and that pull-down resistors are in place.
  • Division by zero error? The code already checks for division by zero. If you attempt it, the message “Error: Division by zero” will be displayed on the Serial Monitor.
  • No output on the Serial Monitor? Ensure the baud rate in the Serial Monitor is set to 9600, and verify the correct COM port is selected.

Further Exploration

  • Add More Numbers: Expand the project to include buttons for numbers 0-9 to perform more complex calculations.
  • Add an LCD Display: Display the result on an LCD screen instead of using the Serial Monitor.
  • Add a Clear Button: Add a button to reset the numbers and start over.

Note

This project introduces important concepts such as digital inputs, if-else conditions, and switch cases. By creating this Arduino calculator, you’ll learn how to manage user input, perform arithmetic operations, and output results via the Serial Monitor.

FAQ

Q1: How do the pull-down resistors work?
The pull-down resistors ensure that the pins connected to the buttons are pulled to LOW when the buttons are not pressed, preventing floating values.

Q2: What is the role of switch case in this project?
The switch case statement allows the Arduino to perform different arithmetic operations based on the operation selected by the user.

Q3: Can I use more numbers for calculation?
Yes, you can modify the project to include more buttons for numbers 0-9, making the calculator capable of handling more complex operations.

Q4: How can I display the result on an LCD?
To display the result on an LCD screen, you can connect an LCD to the Arduino and use the LiquidCrystal library to show the calculation result.