Microcontroller 101: Difference between revisions

From XPUB & Lens-Based wiki
(13 intermediate revisions by the same user not shown)
Line 5: Line 5:
</pre>
</pre>


=09-04-2024 e͎l͎e͎c͎t͎r͎i͎c͎=
=09-04-24 e͎l͎e͎c͎t͎r͎i͎c͎ t͎r͎i͎c͎k͎s͎=


https://pzwiki.wdka.nl/mw-mediadesign/images/7/7e/Cricket.mp4
==LOLIN 32 Installation party :~)==
 
sensors and actuators
 
 
 
==introduction to Arduino==
 
===LOLIN 32 Installation party :~)===


[[File:Lolin32.jpeg|200px|thumb|left|Wemos LOLIN32 - ESP32]]
[[File:Lolin32.jpeg|200px|thumb|left|Wemos LOLIN32 - ESP32]]
Line 29: Line 21:


_windows: open Device Manager >> find UART device >> right click and update driver >> select driver
_windows: open Device Manager >> find UART device >> right click and update driver >> select driver
==Arduino code==


===Hello World!===
===Hello World!===
Line 68: Line 62:
}
}
</pre>
</pre>
==== Traffic light example ====
=== Traffic light example ===


[[File:Trafficjam.png|thumb|center]]
[[File:Trafficjam.png|thumb|center]]
Line 99: Line 93:
</pre>
</pre>


==== LDR example ====
=== LDR example ===
example with a light resistor.
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)
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)
Line 120: Line 114:
</pre>
</pre>


==== LDR & speaker example ====
=== LDR & speaker example ===
[[File:Ldrspeaker.png|thumb|center]]
[[File:Ldrspeaker.png|thumb|center]]
[[File:Ldrspeakerschematics.png|thumb|center]]
[[File:Ldrspeakerschematics.png|thumb|center]]
Line 141: Line 135:
</pre>
</pre>


==== Poti example ====
=== Poti example ===
[[File:Poti.png|thumb|center]]
[[File:Poti.png|thumb|center]]
[[File:Potischematics.png|thumb|center]]
[[File:Potischematics.png|thumb|center]]
Line 159: Line 153:
</pre>
</pre>


==== Poti & speaker example ====
=== Poti & speaker example ===
[[File:Potispeaker.png|thumb|center]]
[[File:Potispeaker.png|thumb|center]]
[[File:Potispeakerschematics.png|thumb|center]]
[[File:Potispeakerschematics.png|thumb|center]]
Line 181: Line 175:
</pre>
</pre>


==== Button & alarm example ====
=== Button & alarm example ===
[[File:Buttonspeaker.png|thumb|center]]
[[File:Buttonspeaker.png|thumb|center]]
[[File:Buttonschematics.png|thumb|center]]
[[File:Buttonschematics.png|thumb|center]]
Line 213: Line 207:
</pre>
</pre>


=16-04-24=
=16-04-24 ๓໐t໐rŞ + ŞēຖŞ໐rŞ = r໐๖໐t=
===how to document===
===motors===
>> template:
===sensors===
====humidity====
[[File:Dht22.jpg|200px]]
the DHT22 humidity sensor :)
<pre>
#include <DHT.h>;


====title====
//Constants
This sketch does this
#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


====components list====
>>for example:
Wemos Lolin32<br>
Led<br>
220ohm resistor<br>
type of sensor<br>


====schematic drawing====
//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.
}
</pre>


[[File: your pic]]
====sound====
[[File:sound_sensor.jpg|200px]]


====arduino code====
KY-037 Sound Detection Sensor Module :::;;-))
<pre>
<pre>
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);
  }
}
</pre>
====soil moisture====
[[File:Soil moisture sensor.jpg|200px]]
Capacitive Soil Moisture Sensor Module :~)
<pre>
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
}
</pre>
</pre>


=23-04-24 l͓̽i͓̽n͓̽k͓̽i͓̽n͓̽g͓̽ t͓̽h͓̽e͓̽ r͓̽e͓̽a͓̽l͓̽m͓̽s͓̽=
=23-04-24 l͓̽i͓̽n͓̽k͓̽i͓̽n͓̽g͓̽ t͓̽h͓̽e͓̽ r͓̽e͓̽a͓̽l͓̽m͓̽s͓̽=

Revision as of 23:38, 15 April 2024


ḿ̬̏ͤͅỉ͔͖̜͌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 :~)

Wemos LOLIN32 - ESP32
Pinout LOLIN D32 PRO

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

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

Simple Led Blink Example
Simple Led Blink Example Schematics

// 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

Trafficjam.png
Trafficjamschematics.png
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)

LDR example.png
LDRschematics.png
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

Ldrspeaker.png
Ldrspeakerschematics.png
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

Poti.png
Potischematics.png
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

Potispeaker.png
Potispeakerschematics.png
//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

Buttonspeaker.png
Buttonschematics.png
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

Dht22.jpg the DHT22 humidity sensor :)

#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

File:Sound sensor.jpg

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

Soil moisture sensor.jpg 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
}

23-04-24 l͓̽i͓̽n͓̽k͓̽i͓̽n͓̽g͓̽ t͓̽h͓̽e͓̽ r͓̽e͓̽a͓̽l͓̽m͓̽s͓̽