Microcontroller 101
ḿ̬̏ͤͅỉ͔͖̜͌c͕͗ͤ̕̕r̴̨̦͕̝o̯̱̊͊͢c͕͗ͤ̕̕o̯̱̊͊͢ṇ̤͛̒̍t̲̂̓ͩ̑r̴̨̦͕̝o̯̱̊͊͢l̙͖̑̾ͣl̙͖̑̾ͣẹ̿͋̒̕r̴̨̦͕̝ 1̨̹̦͍̀0̗̜͕̅̃1̨̹̦͍̀
09-04-24 e͎l͎e͎c͎t͎r͎i͎c͎ t͎r͎i͎c͎k͎s͎
LOLIN 32 Installation party :~)
more info about the lolin32 is here: https://www.wemos.cc/en/latest/d32/d32_pro.html
step 1: download Arduino software
step 2: download driver for microcontroller
step 3: install driver
_windows: open Device Manager >> find UART device >> right click and update driver >> select driver
step 4: install esp32 library in the board manager
Arduino code
Hello World!
in the Arduino program, select the correct board (WEMOS LOLIN32) and select the USB port you are using (if you are unsure, check the listed ports, unplug and see what changed)
void setup() { Serial.begin(115200); } void loop() { Serial.println("Hello World!"); //sends a message to the computer }
Simple Led blink example
// always use a "preresistor" with the LED, because the 5v coming from the microcontroller is too much // a LED only consumes ~2.5 volt, the resistor the other volt // int ledPin = 23; //the int ledPin is 13 void setup() { pinMode(ledPin,OUTPUT); //ledPin is a OUTPUT } void loop() { digitalWrite(ledPin,HIGH); //turns pin 13 on delay(500); //stops the loop for 500 milliseconds digitalWrite(ledPin,LOW); //turns pin 13 off delay(500); //stops the loop for 500 milliseconds }
Traffic light example
int RedLedPin = 23; //the int RedLedPin is 13 int GreenLedPin = 22; //the int GreenLedPin is 12 void setup() { pinMode(RedLedPin,OUTPUT); //ledPin is a OUTPUT pinMode(GreenLedPin,OUTPUT); //ledPin is a OUTPUT } void loop() { digitalWrite(GreenLedPin,HIGH); //turns green led on delay(5000); //stops the loop for 5000 milliseconds for(int i = 0; i < 5; i++){ //this for loop gets 5 times repeated digitalWrite(GreenLedPin,LOW); //turns green led off delay(500); //stops the loop for 500 milliseconds digitalWrite(GreenLedPin,HIGH); //turns green led off delay(500); //stops the loop for 500 milliseconds } digitalWrite(GreenLedPin,LOW); //turns green led off digitalWrite(RedLedPin,HIGH); //turns red led on delay(5000); //stops the loop for 5000 milliseconds digitalWrite(RedLedPin,LOW); //turns red led on }
LDR example
example with a light resistor. keep in mind the LDR pin needs to be a pin with a ADC(analog to digital converter), because you check the analog voltage. on the arduino these are the ANALOG IN pins. on the ESP32 the pins with the ADC(check the pinout graphic)
int LDR = 34; //the LDR pin void setup() { Serial.begin(115200); pinMode(LDR,INPUT); //LDR is an INPUT } void loop() { int value = analogRead(LDR); // read the analog value of the LDR Serial.println(value); // print the value of the LDR, open serial monitor delay(10); }
LDR & speaker example
int LDR = 34; //the LDR pin int speaker = 23; void setup() { Serial.begin(115200); pinMode(LDR,INPUT); //LDR is an INPUT pinMode(speaker,OUTPUT); //speaker is a OUTPUT } void loop() { int value = analogRead(LDR); // read the analog value of the LDR Serial.println(value); // print the value of the LDR, open serial monitor tone(speaker,value); //create a frequency on the speaker pin; the frequency hertz is the value delay(10); }
Poti example
int poti = 34; //the poti pin void setup() { Serial.begin(115200); pinMode(poti,INPUT); //poti is an INPUT } void loop() { int value = analogRead(poti); // read the analog value of the poti Serial.println(value); // print the value of the poti, open serial monitor delay(10); }
Poti & speaker example
//keep in mind you can only use a mini speaker. for bigger speakers you need an amplifier. int poti = 34; //the pto pin int speaker = 23; void setup() { Serial.begin(115200); pinMode(poti,INPUT); //poti is an INPUT pinMode(speaker,OUTPUT); //speaker is a OUTPUT } void loop() { int value = analogRead(poti); // read the analog value of the poti Serial.println(value); // print the value of the poti, open serial monitor tone(speaker,value); //create a frequency on the speaker pin; the frequency hertz is the value delay(10); }
Button & alarm example
int button = 34; int speaker = 23; void setup() { Serial.begin(115200); //make usb connection pinMode(button, INPUT); //button is an INPUT pinMode(speaker, OUTPUT); //speaker is an OUTPUT } void loop() { bool value = digitalRead(button); //read the digital value of button if(button){ Serial.println("start alarm"); for(int repeter = 0; repeter<5; repeter++){ //repeat 5 times for(int frequency = 500; frequency<1200; frequency++){ //count from 500 to 1200 tone(speaker, fequency); //generate the frequency on the speaker pin delay(5); } notone(speaker); //turn off the speaker pin delay(500); // } }else{ Serial.println("button not pressed"); } }
16-04-24 ๓໐t໐rŞ + ŞēຖŞ໐rŞ = r໐๖໐t
motors
sensors
humidity
#include <DHT.h>; //Constants #define DHTPIN 21 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino //Variables int chk; float hum; //Stores humidity value float temp; //Stores temperature value void setup() { Serial.begin(9600); dht.begin(); } void loop() { //Read data and store it to variables hum and temp hum = dht.readHumidity(); temp= dht.readTemperature(); //Print temp and humidity values to serial monitor Serial.print("Humidity: "); Serial.print(hum); Serial.print(" %, Temp: "); Serial.print(temp); Serial.println(" Celsius"); delay(2000); //Delay 2 sec. }
sound
KY-037 Sound Detection Sensor Module :::;;-))
int sensorPin = 21; // select the input pin for the potentiometer a0 on board, D1 pinout not connected int sensorValue = 0; // variable to store the value coming from the sensor void setup() { // declare the ledPin as an OUTPUT: Serial.begin(9600); pinMode(22, OUTPUT); // } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); // turn the ledPin on Serial.println(sensorValue); if (sensorValue>448) { Serial.println("high detected"); digitalWrite(22, HIGH); Serial.println("Blink On"); delay(2000); digitalWrite(22, LOW); } else { digitalWrite(22, LOW); } }
soil moisture
Capacitive Soil Moisture Sensor Module :~)
const int dry = 595; // Value for dry sensor const int wet = 239; // Value for wet sensor void setup() { Serial.begin(9600); // Initialize serial communication } void loop() { int sensorVal = analogRead(26); // Read moisture sensor value from pin A0 int percentageHumidity = map(sensorVal, wet, dry, 0, 100); // Convert sensor value to percentage Serial.print(percentageHumidity); // Print percentage humidity value Serial.println("%"); // Print % symbol delay(100); // Delay for stability }
sonar distance
*/ // defines pins numbers const int trigPin = 9; const int echoPin = 10; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); }