LAB 2
MUHAMMAD FAIRUZ BIN IMRAN (192020180)
DIPLOMA COMPUTER ENGINEERING (R2404)
NDJ 30003 - ELECTRONIC INSTRUMENTATION
LAB 2 - EXPLORING THE ESP32 GPIOs
INTRODUCTION
We may connect the circuit below before implementing the experiment below, making it easy to interact with the code and see the results. The following are the components that will be used:
i) ESP32 DOIT DEVKIT V1 Board
ii) 5mm LED
iii) 330 Ohm resistor
iv) Pushbutton
v) 10k Ohm resistor
vi) Breadboard
vii) Jumper wires
UNIT 1: ESP32 Digital Inputs and Outputs
1) Copy the code to your Arduino IDE after the circuit is complete.
// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 16; // the number of the LED pin
// variable for storing the pushbutton status
int buttonState = 0;
void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(ledPin, HIGH);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
}
}
2) Upload the sketch to the ESP32 board.
3) You should now see that the LED is turned on even if you are not pushing the button.
4) The LED should turn off when you click the button.
5) The status of the LED may be viewed in the Arduino IDE serial monitor and serial plotter. Make sure that the baud rate is set to 115200.
i) When the button is not pushed,
ii) When the button is pushed,
6) View the full video of the real-time demonstration for this unit.
UNIT 2: ESP32 Touch Sensor
1) To perform this experiment, pin 4 that is connected to the button module will be pulled out so that the tip of the jumper can be used as a touch sensor.
2) Now, paste the following code into your Arduino IDE.
// set pin numbers
const int touchPin = 4;
const int ledPin = 16;
// change with your threshold value
const int threshold = 20;
// 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);
}
3) Upload the sketch to the ESP32 board.
4) When pin 4 is not contacted by a finger, the LED remains off since there is a particular threshold for the LED to turn on.
5) The LED will turn on if you hold the tip of the jumper since it is less than the threshold value.
6) In the serial monitor, you can see the value and status of the LED when it is touched or not by the jumper tip.
7) Using the Serial Plotter, you can see the value changing more clearly. In the upper part of your Arduino IDE, find Tools > Serial Plotter. Then it will open this window.
8) View the full video of the real-time demonstration for this unit.
UNIT 3: ESP32 Pulse-Width Modulation (PWM)
i) Dimming an LED
1) Now we'll see whether we can make an LED gradually raise and gradually reduce its brightness.
2) In your Arduino IDE, paste the code below.
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO16
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
}
3) If you watch the LED, you will see that it will gradually brighten and then dim. A picture of the outcome is not possible. As a result, watch the entire video of the real-time demonstration.
ii) Getting the same signal on different GPIOs
1) It's the same as the last demonstration; the only difference is that it uses additional LEDs to generate the PWM signal.
2) In your Arduino IDE, paste the code below.
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO16
const int ledPin2 = 17; // 17 corresponds to GPIO17
const int ledPin3 = 5; // 5 corresponds to GPIO5
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
ledcAttachPin(ledPin2, ledChannel);
ledcAttachPin(ledPin3, ledChannel);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
}
3) If you look at the three LEDs, you'll see that they gradually increase in brightness and gradually decrease. A picture of the outcome is not possible. As a result, watch the entire video of the real-time demonstration.
UNIT 4: ESP32 Reading Analog Inputs
With an ESP32, you may measure changing voltage levels between 0V and 3.3V by reading an analogue value. After then, a value between 0 and 4095 will be assigned to the voltage measurement.
1) Now let's look at a basic example of reading an analogue value from a potentiometer.
2) In your Arduino IDE, paste the code below.
const int potPin = 35;
// variable for storing the potentiometer value
int potValue = 0;
void setup() {
Serial.begin(115200);
delay(1000);
}
void loop() {
// Reading potentiometer value
potValue = analogRead(potPin);
Serial.println(potValue);
delay(500);
}
3) When you open the serial monitor, you'll notice that when the potentiometer arrow points to the left, the value is at its lowest, which is 0.
4) The value will be the maximum, which is roughly 4095 if the potentiometer arrow points to the right.
5) When you adjust the potentiometer, the serial monitor will display the value.
6) View the full video of the real-time demonstration for this unit.
UNIT 5: ESP32 Hall Effect Sensor
The ESP32 has a built-in hall effect sensor, which is located behind the metal cap.
A hall effect sensor can detect changes in the magnetic field in its surroundings. The higher the magnitude of the output voltage, the stronger the magnetic field.
1) Copy the code below into your Arduino IDE to read the hall effect sensor measurements with the ESP32.
// Simple sketch to access the internal hall effect detector on the esp32.
// values can be quite low.
// Brian Degger / @sctv
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);
}
2) You may put it to the test by using a magnet. Depending on the magnetic poles, the value will change to either negative or positive. Examine the output on the serial monitor. Make sure to set the baud rate to 9600.
3) View the full video of the real-time demonstration for this unit.
UNIT 6: ESP32 Light-Dependent Resistor
A light-dependent resistor is a passive component that reduces resistance in response to the received luminance or light on the component's sensitive surface. The lower the degree of resistance, the denser the light received by the LDR.
1) Assemble the circuit in the same way you did at the start of the experiment.
2) In your Arduino IDE, paste the code below.
/* LDR Luximeter */
// Constants
#define VIN 3.3 // V power voltage, 3.3v in case of NodeMCU
#define R 10000 // Voltage divider resistor value
// Parameters
const int Analog_Pin = 34; // Analog pin A0
float RLDR = 0;
//Variables
int LDR_Val; // Analog value from the LDR
int Iluminance; //Lux value
int conversion(int raw_val);
void setup(void) {
Serial.begin(115200);
}
void loop(void) {
LDR_Val = analogRead(Analog_Pin);
Iluminance = conversion(LDR_Val);
Serial.print("Analog reading from sensor = ");
Serial.println(LDR_Val); // between 0 -
Serial.print("Calculate R of LDR = ");
Serial.print(RLDR);
Serial.println(" Ohm");
Serial.print("Iluminance value = ");
Serial.print(Iluminance); // Converted value
Serial.println(" Lux");
Serial.println();
delay(500);
}
int conversion(int raw_val){
// Conversion rule
float Vout = float(raw_val) * (VIN / float(4095));// Conversion analog to voltage
RLDR = (R * Vout)/(VIN - Vout); // Conversion voltage to resistance in Ohm
int lux = 170/(RLDR/1000); // Conversion resitance to lumen, RLDR/1000 - convert Ohm to KOhm
return lux;
}
3) Set the baud rate to 115200 in the serial monitor.
4) Adjust the brightness of the surroundings to test the LDR.
5) Examine the output in the serial monitor.
6) View the full video of the real-time demonstration for this unit.
Comments
Post a Comment