HOW to Make a Heat Dissipation Device with ESP32
Description
We will use a temperature sensor and some modules to make a smart cooling device in this experiment. When the ambient temperature is higher than a certain value, the motor is turned on, thereby reducing the ambient temperature and achieving the heat dissipation effect. Then display the temperature value in the four-digit segment display.
Required Components
|
|
|
|
---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
keyestudio 130 Motor*1 |
Keyestudio TM1650 4-Digit Segment Display*1 |
|
|
|
|
Keyestudio 18B20 Temperature Sensor*1 |
3P Dupont Wire*1 |
4P Dupont Wire*2 |
Micro USB Cable*1 |
|
|
|
|
Battery Holder*1 |
Battery(provided by yourself)*6 |
|
|
Connection Diagram
Test Code
//**********************************************************************************
/*
* Description : DS18B20 controls a four digit tube and a motor that simulates Heat Abstractor
* Auther : http//www.keyestudio.com
*/
#include <DS18B20.h>
#include "TM1650.h" //Import the TM1650 library file
//The two ports are GP21 and GP22
#define DIO 21
#define CLK 22
TM1650 DigitalTube(CLK,DIO);
//ds18b20 pin to 13
DS18B20 ds18b20(13);
void setup() {
Serial.begin(9600);
DigitalTube.setBrightness(); //set brightness, 0---7, default : 2
DigitalTube.displayOnOFF(); //display on or off, 0=display off, 1=display on, default : 1
for(char b=1;b<5;b++){
DigitalTube.clearBit(b); //DigitalTube.clearBit(0 to 3); Clear bit display.
}
// DigitalTube.displayDot(1,true); //Bit0 display dot. Use before displayBit().
DigitalTube.displayBit(1,0); //DigitalTube.Display(bit,number); bit=0---3 number=0---9
//Motor is connected to 15 4
pinMode(15, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
double temp = ds18b20.GetTemp();//Read the temperature
temp *= 0.0625;//The conversion accuracy is 0.0625/LSB
Serial.println(temp);
displayFloatNum(temp);//4- digit tube display temperature value
if (temp > 25) {//When the temperature exceeds 25 degrees Celsius, turn on the fan
digitalWrite(15, LOW);
digitalWrite(4, HIGH);
} else {//Otherwise, turn off the fan.
digitalWrite(15, LOW);
digitalWrite(4, LOW);
}
delay(100);
}
void displayFloatNum(float temp){
if(temp > 9999)
return;
int dat = temp*10;
//DigitalTube.displayDot(2,true); //Bit0 display dot. Use before displayBit().
if(dat/10000 != 0){
DigitalTube.displayBit(1, dat%100000/10000);
DigitalTube.displayBit(2, dat%10000/1000);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
if(dat%10000/1000 != 0){
DigitalTube.clearBit(1);
DigitalTube.displayBit(2, dat%10000/1000);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
if(dat%1000/100 != 0){
DigitalTube.clearBit(1);
DigitalTube.clearBit(2);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
DigitalTube.clearBit(1);
DigitalTube.clearBit(2);
DigitalTube.clearBit(3);
DigitalTube.displayBit(4, dat%100/10);
}
//**********************************************************************************
Code Explanation
The setting of variables and the storage of detection values are the same as what we learned earlier. We also set a temperature threshold and control the rotation of the motor when the threshold is exceeded, and then we use the digital tube to display the temperature value.
Test Result
Connect the wires according to the experimental wiring diagram and power on. Switch the DIP switch on the ESP32 expansion board to the ON end, compile and upload the code to the ESP32. After uploading successfully, we can see the temperature of the current environment (unit is Celsius) on the four-digit segment display, as shown in the figure below. If this value exceeds the value we set, the fan will rotate to dissipate heat.