Build together ESP32 Internal Sensor (touch, hall-effect, temperature)

Hello, future engineers!!

Welcome again to my blog!

My name is Muhamad Fariz Ramadhan from STI 2020 ITB. I made this blog to help you guys out about embedded system especially setting up ESP32 for getting some conditions. At This time, I am going to show you to activate or build ESP32 internal sensor.

In this modern era, we can see a lot of thing that already used sensor as their part, for example automatic wastafel or washbasin.

source : Rezeki Abadi

It's more complicated than sensor that will we discuss in this article. So the first one, let's we learn sensor that more simple and basic, that is sensor that already available in ESP32.

*DON'T YOU KNOW??*
ESP32 has sensors inside itself, those are touch sensor, hall-effect sensor, temperature sensor. So, in some conditions we don't need to buy extra sensor indeed- just use the capability of ESP32.

We only need 5 requirements to do the tutorial.
1. ESP32
2. Breadboard
3. Jumper wires
4. Magnet
5. Arduino IDE (Software)

1. TOUCH SENSOR

Touch sensor can sense variations of eletrical change, including human skin

The Circuit:


Place the ESP32 to the breadboard, and put the jumper male to male to any GPIO (in here I was using GPIO 15) and let the other tip hang free as the touched area.

The code (File > Examples > ESP32 > Touch > TouchRead):

void setup()
{
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Test");
}

void loop()
{
  Serial.println(touchRead(15));  // get value using T0
 delay(1000);

The Demonstration:
sensor untouched.


sensor touched

From the picture above, we can see that when the sensor didn't touched the serial monitor will display >50 value of number. But, when the sensor touched it will get decreased to the range of 20.

2. TEMPERATURE SENSOR

Temperature Sensor is used to monitor its core temperature that effected by the environment.

The Circuit:
















Just put ESP32 to the bread board.

The code:
 
#ifdef __cplusplus
  extern "C" {
 #endif

  uint8_t temprature_sens_read();

#ifdef __cplusplus
}
#endif

uint8_t temprature_sens_read();

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print("Temperature: ");
  
  // Convert raw temperature in F to Celsius degrees
  Serial.print((temprature_sens_read() - 32) / 1.8);
  Serial.println(" C");
  delay(1000);
}

The Demonstration:


At this point, we can see that temperature of the ESP32's core is 53.33 cellcius degree.

3. HALL-EFFECT SENSOR

Hall-effect sensor is used to detect changes in the magnetic field in its surroundings.

The Circuit:

Just put the ESP32 to a bread board and prepare magnet for the simulation.

The code (File > Examples > ESP32 > Hallsensor):

int val = 0;

void setup() {
  Serial.begin(9600);
}

// put your main code here, to run repeatedly
void loop() {
  // read hall effect sensor value
  val = hallRead();
  // print the results to the serial monitor
  Serial.println(val); 
  delay(1000);
}

The Demonstration:

magnet far from ESP32 

magnet surround ESP32

At this point, we can see that when the magnet surrounding the ESP32 the number that displayed of serial monitor will show high number such as 1134, 364, 839 (from the picture above). But when the magnet get away from ESP32 the number that displayed just around 0.

BONUS:
Are you strong enough to turn on the LEDs?

source: VectorStock


In this bonus section, we'd like to make an automatic LED that turn on by touch sensor. It is so easy and need some requirement that we've done before so you guys don't need to buy somethings more.

Here is the schematic circuit to make automatic LED with touch sensor.




Descriptions:
1. Link GND to the negative board
2. Link GPIO 16 to the resistor
3. Link LED(+) to the resistor and link LED(-) to the negative board
4. Hang GPIO 4 out to become our touch sensor.







Here is the result of mine following the schematic above.



Then for the code, copy this to your IDE and upload to the ESP32

// set pin numbers
const int touchPin = 4; 
const int ledPin = 16;

// change with your threshold value
const int threshold = 25;
// variable for storing the touch pin value 
int touchValue;

void setup(){
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  // initialize the LED pin as an output:
  pinMode (ledPin, OUTPUT);
}

void loop(){
  // read the state of the pushbutton value:
  touchValue = touchRead(touchPin);
  Serial.print(touchValue);
  // check if the touchValue is below the threshold
  // if it is, set ledPin to HIGH
  if(touchValue < threshold){
    // turn LED on
    digitalWrite(ledPin, HIGH);
    Serial.println(" - LED on");
  }
  else{
    // turn LED off
    digitalWrite(ledPin, LOW);
    Serial.println(" - LED off");
  }
  delay(500);
}

Here the results after the code get uploaded.
desc : the sensor doesn't get touched

desc : the sensor get touched

Explanation: How does it can turn on a LED with touch sensor?

Basically, from the code above there is 2 variables that effect the works of circuit:
  1. touchValue => the value of how hard the sensor get touched, in form of number. Harder the sensor get touched, more minimum value of the variable would get.
  2. threshold => the constant number that linked to LED on/off status. (the number should be in touchValue's range)
Then, in the code we add:
 if(touchValue < threshold){
    // turn LED on
    digitalWrite(ledPin, HIGH);
    Serial.println(" - LED on");
This part of code give the instruction to ESP32 that if the touchValue lower than threshold so the LED should be turn on. In order to get touchValue much lower we have to touch the sensor harder, that's how it works!

*MORE ADVANCE*

What if we add more lamp like 3 LEDs that will light up differently according to how hard we touch the sensors. The more power we have so the more LEDs that we can turns on up, sounds interesting isn't it? 
Then let's make it!

The circuit is just like before but we add more 2 LEDs. I'm using GPIO 5 and GPIO 18 for the new Ledpin.
Here is the picture of mine.

Upload the code
// set pin numbers
const int touchPin = 4; 
const int ledPin1 = 16;
const int ledPin2 = 5;
const int ledPin3 = 18;

// change with your threshold value
const int threshold1 = 44;
const int threshold2 = 25;
const int threshold3 = 18;
// variable for storing the touch pin value 
int touchValue;

void setup(){
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  // initialize the LED pin as an output:
  pinMode (ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
}

void loop(){
  // read the state of the pushbutton value:
  touchValue = touchRead(touchPin);
  Serial.println(touchValue);
  // check if the touchValue is below the threshold
  // if it is, set ledPin to HIGH
  if(touchValue < threshold1){
    // turn LED1 on
    digitalWrite(ledPin1, HIGH);
    Serial.println(" - LED1 on");
  }
  else{
    // turn LED1 off
    digitalWrite(ledPin1, LOW);
    Serial.println(" - LED1 off");
  }

  if(touchValue < threshold2){
    // turn LED2 on
    digitalWrite(ledPin2, HIGH);
    Serial.println(" - LED2 on");
  }
  else{
    // turn LED2 off
    digitalWrite(ledPin2, LOW);
    Serial.println(" - LED2 off");
  }

  if(touchValue < threshold3){
    // turn LED3 on
    digitalWrite(ledPin3, HIGH);
    Serial.println(" - LED3 on");
  }
  else{
    // turn LED3 off
    digitalWrite(ledPin3, LOW);
    Serial.println(" - LED3 off");
  }
  delay(500);
}

Of course we add 2 more variable threshold differently to each of the LED, so they will not bulb on at the same valueTouch. In my condition, I'm using 44, 25, and 18 according to sensor's range of valueTouch.

Upload the code and then the circuit would be like this.


That's all. Thank you guys.
Semoga bermanfaat!!
- Fariz STI'20 


 


 

Komentar

Postingan populer dari blog ini

ESP32 with Multiple I2C Devices

ESP32 Web Server : Turn on LED using WiFi from Your Mobile or Desktop

Displayed cute picture of cat with OLED Display on ESP32 + PWM