Contoh Program Arduino Sederhana dengan Tinkercad dan ESP32 (Menggunakan Arduino IDE)

Assalamu‘alaikum wr. wb.

Hello guys, Kembali lagi bersama Inzaghi's Blog! Jika sebelumnya sudah pernah membahas tentang Modul Dasar Arduino, sekarang waktunya melakukan Implementasi Program Aplikasi Arduino Sederhana menggunakan Tinkercad dan ESP32 melalui Arduino IDE.

Ilustrasi Praktikum Arduino dengan ESP32 dan Tinkercad



TUTORIAL CARA MENGGUNAKAN TINKERCAD

Pertama, merilah kita buka Situs Tinkercad di sini (Tinkercad.com), dan klik "Log in".


Kemudian, Masuklah menggunakan Akun Google. Setelah itu, pilihlah "Personal Accounts" untuk memilih Akun Pribadi.


Setelah itu, isilah Data Pribadi seperti Negara dan Tanggal Lahir, dan klik "Next".


Selanjutnya, klik "Continue" untuk Syarat dan Ketentuan dari Autodesk.


Anda sudah masuk ke Tampilan Dashboard dari Tinkercad. Jika ingin membuat Rangkaian Arduino, silakan klik "Create your First Circuit Design" pada Circuits.


Setelah itu, Rangkailah sesuka hati Anda. Dan inilah hasilnya setelah dijalankan :




MEMBUAT RANGKAIAN DAN KODE ARDUINO DENGAN MENGGUNAKAN TINKERCAD

Sumber Tutorial (Arduino Uno) : Arduinogetstarted.com dan Tutorialspoint.com
Sumber Tutorial Lainnya (ESP32) : ESP32io.com

Dan inilah beberapa Project Arduino Uno yang telah kami buat di Tinkercad :

1. Arduino Dasar

1. Hello World

Kode :

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Hello World!");
}

void loop() {
  // put your main code here, to run repeatedly:
}
Gambar :


2. On/Off Command (Serial Monitor)

Kode :

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT); // set the digital pin as output:
}

void loop() {
  if(Serial.available()) // if there is data comming
  {
    String command = Serial.readStringUntil('\n'); // read string until meet newline character

    if(command == "ON"){
      digitalWrite(LED_BUILTIN, HIGH); // turn on command
      Serial.println("Turned ON"); // send action to Serial Monitor
    }

    else if(command == "OFF"){
      digitalWrite(LED_BUILTIN, LOW);  // turn off command
      Serial.println("Turned OFF"); // send action to Serial Monitor
    }
  }
}
Gambar :


3. Analog Waves (Serial Plotter)

Kode :

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

void loop() {
    int y1 = analogRead(A0);
   
    Serial.println(y1);
   
    delay(100);
}
Gambar :


2. Lampu LED

1. LED Dasar Tanpa Breadboard (Blinking LED Without Breadboard)

Gambar :


Kode : 

// C++ code
//
void setup(){
    pinMode(9, OUTPUT);
}

void loop(){
    digitalWrite(9, HIGH);
    delay(1000); // Wait for 1000 millisecond(s)
    digitalWrite(9, LOW);
    delay(1000); // Wait for 1000 millisecond(s)
}
2. LED Dasar dengan Breadboard (Blinking LED with Breadboard)

Gambar :


Kode : 

int LED = 2;

void setup(){
  pinMode(LED, OUTPUT);
}

void loop(){
  digitalWrite(LED, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(LED, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}
3. LED Breadboard dengan Tombol (Blinking LED with Button)

Gambar :


Kode : 

// constants won't change:
const int LED_PIN = 4;     // the number of the LED pin
const int BUTTON_PIN = 8;  // the number of the button pin

const long BLINK_INTERVAL = 1000;   // interval at which to blink LED (milliseconds)

// Variables will change:
int ledState = LOW;   // ledState used to set the LED

int previousButtonState = LOW; // will store last time button was updated

unsigned long previousMillis = 0;   // will store last time LED was updated

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

  // set the digital pin as output:
  pinMode(LED_PIN, OUTPUT);
 
  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= BLINK_INTERVAL) {
    // if the LED is off turn it on and vice-versa:
    ledState = (ledState == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN, ledState);

    // save the last time you blinked the LED
    previousMillis = currentMillis;
  }

  // check button state's change
  int currentButtonState = digitalRead(BUTTON_PIN);

  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);

    // save the last state of button
    previousButtonState = currentButtonState;
  }
}
4. LED Breadboard Tanpa Delay (Blinking LED Without Delay)

Gambar :


Kode : 

const int LED_PIN = 3;     // the number of the LED pin
const int BUTTON_PIN = 6;  // the number of the button pin

const long BLINK_INTERVAL = 1000;   // interval at which to blink LED (milliseconds)

// Variables will change:
int ledState = LOW;   // ledState used to set the LED

int previousButtonState = LOW; // will store last time button was updated

unsigned long previousMillis = 0;   // will store last time LED was updated

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

  // set the digital pin as output:
  pinMode(LED_PIN, OUTPUT);
 
  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= BLINK_INTERVAL) {
    // if the LED is off turn it on and vice-versa:
    ledState = (ledState == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN, ledState);

    // save the last time you blinked the LED
    previousMillis = currentMillis;
  }

  // check button state's change
  int currentButtonState = digitalRead(BUTTON_PIN);

  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);

    // save the last state of button
    previousButtonState = currentButtonState;
  }

  // DO OTHER WORKS HERE
}
5. LED Lampu Ganda

Gambar :


Kode : 

int redLED = 3;
int yellowLED = 5;
int greenLED = 8;

void setup()
{
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
}

void loop()
{
  digitalWrite(redLED, HIGH);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, HIGH);
  digitalWrite(greenLED, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
}

3. Lampu LED RGB

Gambar :


Kode : 

const int PIN_RED   = 5;
const int PIN_GREEN = 6;
const int PIN_BLUE  = 7;

void setup() {
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
}

void loop() {
  // color code #00C9CC (R = 0,   G = 201, B = 204)
  analogWrite(PIN_RED,   0);
  analogWrite(PIN_GREEN, 201);
  analogWrite(PIN_BLUE,  204);

  delay(1000); // keep the color 1 second

  // color code #F7788A (R = 247, G = 120, B = 138)
  analogWrite(PIN_RED,   247);
  analogWrite(PIN_GREEN, 120);
  analogWrite(PIN_BLUE,  138);

  delay(1000); // keep the color 1 second

  // color code #34A853 (R = 52,  G = 168, B = 83)
  analogWrite(PIN_RED,   52);
  analogWrite(PIN_GREEN, 168);
  analogWrite(PIN_BLUE,  83);

  delay(1000); // keep the color 1 second
}

4. Potentiometer

1. Potentiometer dengan LED

Gambar :


Kode : 

const int POTENTIOMETER_PIN   = A0; // Arduino pin connected to Potentiometer pin
const int LED_PIN             = 2; // Arduino pin connected to LED's pin
const float VOLTAGE_THRESHOLD = 2.5; // Voltages

void setup() {
  pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  int analogValue = analogRead(POTENTIOMETER_PIN);      // read the input on analog pin
  float voltage = floatMap(analogValue, 0, 1023, 0, 5); // Rescale to potentiometer's voltage

  if(voltage > VOLTAGE_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED
}

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
2. Potentiometer dengan Servo Motor

Gambar :


Kode : 

#include <Servo.h>

// constants won't change
const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin
const int SERVO_PIN         = 8; // Arduino pin connected to Servo Motor's pin
const int ANALOG_THRESHOLD  = 500;

Servo servo; // create servo object to control a servo

void setup() {
  servo.attach(SERVO_PIN);   // attaches the servo on pin 9 to the servo object
  servo.write(0);
}

void loop() {
  int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin

  if(analogValue > ANALOG_THRESHOLD)
    servo.write(90); // rotate servo motor to 90 degree
  else
    servo.write(0);  // rotate servo motor to 0 degree
}

5. Sensor Ultrasonik

1. Sensor Ultrasonik dengan LED

Gambar :


Kode : 

const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 5; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int LED_PIN  = 4; // Arduino pin connected to LED's pin
const int DISTANCE_THRESHOLD = 100; // centimeters

// variables will change:
float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       // initialize serial port
  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(ECHO_PIN, INPUT);  // set arduino pin to input mode
  pinMode(LED_PIN, OUTPUT);  // set arduino pin to output mode
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}
2. Sensor Ultrasonik dengan Servo Motor

Gambar :


Kode : 

#include <Servo.h>

// constants won't change
const int TRIG_PIN  = 2;  // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN  = 3;  // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int SERVO_PIN = 1; // Arduino pin connected to Servo Motor's pin
const int DISTANCE_THRESHOLD = 100; // centimeters

Servo servo; // create servo object to control a servo

// variables will change:
float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       // initialize serial port
  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(ECHO_PIN, INPUT);  // set arduino pin to input mode
  servo.attach(SERVO_PIN);   // attaches the servo on pin 9 to the servo object
  servo.write(0);
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    servo.write(90); // rotate servo motor to 90 degree
  else
    servo.write(0);  // rotate servo motor to 0 degree

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

6. Photoresistor (Sensor Cahaya)

1. Sensor Cahaya Polos

Gambar :


Kode : 

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // reads the input on analog pin A0 (value between 0 and 1023)
  int analogValue = analogRead(A0);

  Serial.print("Analog reading: ");
  Serial.print(analogValue);   // the raw analog reading

  // We'll have a few threshholds, qualitatively determined
  if (analogValue < 10) {
    Serial.println(" - Dark");
  } else if (analogValue < 200) {
    Serial.println(" - Dim");
  } else if (analogValue < 500) {
    Serial.println(" - Light");
  } else if (analogValue < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }

  delay(500);
}
2. Sensor Cahaya dengan LED

Keterangan :
  • 220 Ω Resistor (Lampu LED)
  • 10 kΩ Resistor (Sensor Cahaya Photoresistor)

Gambar :


Kode : 

const int LIGHT_SENSOR_PIN = A5; // Arduino pin connected to light sensor's  pin
const int LED_PIN          = 1;  // Arduino pin connected to LED's pin
const int ANALOG_THRESHOLD = 500;

int analogValue;

void setup() {
  pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

  if(analogValue > ANALOG_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED
}

7. PIR Sensor (Sensor Gerak / Motion Sensor)

1. PIR Sensor dengan LED

Gambar :


Kode : 

const int PIR_SENSOR_PIN = 10;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int LED_PIN        = 4;   // Arduino pin connected to LED's pin
int motionStateCurrent   = LOW; // current  state of motion sensor's pin
int motionStatePrevious  = LOW; // previous state of motion sensor's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(PIR_SENSOR_PIN, INPUT); // set arduino pin to input mode
  pinMode(LED_PIN, OUTPUT);          // set arduino pin to output mode
}

void loop() {
  motionStatePrevious = motionStateCurrent;             // store old state
  motionStateCurrent  = digitalRead(PIR_SENSOR_PIN); // read new state

  if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    digitalWrite(LED_PIN, HIGH); // turn on
  }
  else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    digitalWrite(LED_PIN, LOW);  // turn off
  }
}
2. PIR Sensor dengan Servo Motor

Gambar :


Kode : 

#include <Servo.h>

// constants won't change
const int PIR_SENSOR_PIN = 7; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN      = 6; // Arduino pin connected to servo motor's pin

Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0;          // the current angle of servo motor
int lastMotionState;    // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(PIR_SENSOR_PIN, INPUT); // set arduino pin to input mode
  servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object

  servo.write(angle);
  currentMotionState = digitalRead(PIR_SENSOR_PIN);
}

void loop() {
  lastMotionState    = currentMotionState;             // save the last state
  currentMotionState = digitalRead(PIR_SENSOR_PIN); // read new state

  if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    servo.write(90);
  }
  else if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    servo.write(0);
  }
}

8. Sensor-sensor

1. Sensor Suhu LM35

Gambar :


Kode : 

#define ADC_VREF_mV    5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35       A1

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

void loop() {
  // get the ADC value from the temperature sensor
  int adcVal = analogRead(PIN_LM35);
  // convert the ADC value to voltage in millivolt
  float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
  // convert the voltage to the temperature in Celsius
  float tempC = milliVolt / 10;
  // convert the Celsius to Fahrenheit
  float tempF = tempC * 9 / 5 + 32;

  // print the temperature in the Serial Monitor:
  Serial.print("Temperature: ");
  Serial.print(tempC);   // print the temperature in Celsius
  Serial.print("°C");
  Serial.print("  ~  "); // separator between Celsius and Fahrenheit
  Serial.print(tempF);   // print the temperature in Fahrenheit
  Serial.println("°F");

  delay(1000);
}
2. Sensor Gaya (Force Sensor)

Gambar :


Kode : 

#define FORCE_SENSOR_PIN A4 // the FSR and 10K pulldown are connected to A0

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

void loop() {
  int analogReading = analogRead(FORCE_SENSOR_PIN);

  Serial.print("Force sensor reading = ");
  Serial.print(analogReading); // print the raw analog reading

  if (analogReading < 10)       // from 0 to 9
    Serial.println(" -> no pressure");
  else if (analogReading < 200) // from 10 to 199
    Serial.println(" -> light touch");
  else if (analogReading < 500) // from 200 to 499
    Serial.println(" -> light squeeze");
  else if (analogReading < 800) // from 500 to 799
    Serial.println(" -> medium squeeze");
  else // from 800 to 1023
    Serial.println(" -> big squeeze");

  delay(1000);
}
3. Sensor Kelembaban Tanah (Soil Moisture Sensor)

Gambar :


Kode : 

#define AOUT_PIN A0 // Arduino pin that connects to AOUT pin of moisture sensor
#define THRESHOLD 100 // CHANGE YOUR THRESHOLD HERE

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

void loop() {
  int value = analogRead(AOUT_PIN); // read the analog value from sensor

  Serial.print("Moisture: ");
  Serial.println(value);

  delay(500);


  if (value < THRESHOLD) {
    Serial.print("The soil is DRY (");
  }
  else {
    Serial.print("The soil is WET (");
  }

  Serial.print(value);
  Serial.println(")");

  delay(500);
}

9. LCD Display

1. LCD Display

Gambar :


Kode :

#include <LiquidCrystal.h>

// LCD pins <--> Arduino pins
const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

void setup()
{
  lcd.begin(16, 2); // set up number of columns and rows
}

void loop()
{
  lcd.setCursor(0, 0);         // move cursor to   (0, 0)
  lcd.print("Inzaghi's Blog");        // print message at (0, 0)
  lcd.setCursor(2, 1);         // move cursor to   (2, 1)
  lcd.print("Inzaghi.com"); // print message at (2, 1)
}
2. LCD Sensor Temperatur LM35 dengan Potentiometer

Gambar :


Kode : 

#include <LiquidCrystal.h>

// LCD pins <--> Arduino pins
const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int sensorpin = A0;

void setup(){
lcd.begin(16, 2); // set up number of columns and rows
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Arduino"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Welcome"); // print message at (2, 1)
}

void loop(){
int analogin = analogRead(sensorpin);
float voltage = analogin*5.0/1024;
float temp = (voltage - 0.5)*100;
lcd.clear();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Suhu in C"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print(temp); // print message at (2, 1)
delay(1000);
}

10. Piezo Buzzer

1. Piezo Buzzer dengan Tombol (Piezo Buzzer with Button)

Gambar :


Kode : 

const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int BUZZER_PIN = 2; // Arduino pin connected to Buzzer's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(BUZZER_PIN, OUTPUT);       // set arduino pin to output mode
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read new state

  if (buttonState == LOW) {
    Serial.println("The button is being pressed");
    digitalWrite(BUZZER_PIN, HIGH); // turn on
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(BUZZER_PIN, LOW);  // turn off
  }
}
2. Piezo Buzzer dengan Potentiometer (Piezo Buzzer with Potentiometer)

Gambar :


Kode : 

const int POTENTIOMETER_PIN = A1; // Arduino pin connected to Potentiometer pin
const int BUZZER_PIN        = 2; // Arduino pin connected to Buzzer's pin
const int ANALOG_THRESHOLD  = 500;

void setup() {
  pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin

  if(analogValue > ANALOG_THRESHOLD)
    digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
  else
    digitalWrite(BUZZER_PIN, LOW);  // turn off Piezo Buzzer
}
3. Piezo Buzzer dengan Sensor Ultrasonik (Piezo Buzzer with Ultrasonic Sensor)

Gambar :


Kode : 

const int POTENTIOMETER_PIN = A1; // Arduino pin connected to Potentiometer pin
const int BUZZER_PIN        = 2; // Arduino pin connected to Buzzer's pin
const int ANALOG_THRESHOLD  = 500;

void setup() {
  pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin

  if(analogValue > ANALOG_THRESHOLD)
    digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
  else
    digitalWrite(BUZZER_PIN, LOW);  // turn off Piezo Buzzer
}


ESP32 Pinout :

ESP8266 Pinout :


Dan masih banyak lagi Proyek-proyek Arduino lainnya yang bisa kamu coba dengan menggunakan Tinkercad.


MEMBUAT RANGKAIAN DAN KODE ARDUINO SEDERHANA MENGGUNAKAN ARDUINO IDE

Sumber Tutorial (Referensi) : Enbriandika.blogspot.comRsetiawan.com, dan Iotles.com (LED Arduino)

1. Lampu LED


Alat dan Bahan :
  • ESP32, ESP8266, Arduino Uno, dll. (Pilih salah satunya)
  • Lampu LED
  • Resistor 330 Ohm (Masing-masing 1 Buah untuk 1 Lampu)
  • Kabel Jumper (Female dan Male)

1. Lampu LED Tunggal (1 LED)

void setup() {
  // menginisialisasi pin digital GIOP18 sebagai output.
  pinMode(18, OUTPUT);
  Serial.begin(9600);
}

// fungsi loop berjalan berulang-ulang selamanya
void loop() {
  digitalWrite(18, HIGH); // nyalakan LED
  Serial.println("Lampu ON");
  delay(500);             // tunggu 500 milidetik
  digitalWrite(18, LOW);  // matikan LED
  Serial.println("Lampu OFF");
  delay(500);             // tunggu 500 milidetik
}
Output :


2. Lampu LED Ganda (2 LED)

int led1 = 18;
int led2 = 19;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
}
Output :


2. Lampu LED RGB


Alat dan Bahan :
  • ESP32, ESP8266, Arduino Uno, dll. (Pilih salah satunya)
  • Lampu LED RGB
  • Resistor 330 Ohm (Minimal 3 untuk 1 Lampu)
  • Kabel Jumper (Female dan Male)

#define PIN_RED    35
#define PIN_GREEN  32
#define PIN_BLUE   33

void setup() {
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
}

void loop() {
  // color code #00C9CC (R = 0,   G = 201, B = 204)
  analogWrite(PIN_RED,   0);
  analogWrite(PIN_GREEN, 201);
  analogWrite(PIN_BLUE,  204);

  delay(1000); // keep the color 1 second

  // color code #F73D8A (R = 247, G = 61, B = 138)
  analogWrite(PIN_RED,   247);
  analogWrite(PIN_GREEN, 61);
  analogWrite(PIN_BLUE,  138);

  delay(1000); // keep the color 1 second

  // color code #34A853 (R = 52,  G = 168, B = 83)
  analogWrite(PIN_RED,   52);
  analogWrite(PIN_GREEN, 168);
  analogWrite(PIN_BLUE,  83);

  delay(1000); // keep the color 1 second
}
Output :


3. Sensor-sensor

1. Sensor Ultrasonik

Alat dan Bahan :
  • ESP32, ESP8266, Arduino Uno, dll. (Pilih salah satunya)
  • Sensor Ultrasonik
  • Resistor 330 Ohm
  • Kabel Jumper (Female dan Male)

int led1 = 18;
int led2 = 19;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
}
Output :



2. PIR Sensor dengan LED

Alat dan Bahan :
  • ESP32, ESP8266, Arduino Uno, dll. (Pilih salah satunya)
  • Sensor Gerak (PIR Sensor)
  • Resistor 330 Ohm
  • Kabel Jumper (Female dan Male)

int pirSensor = 18;
int ledPin = 19;

void setup() {
  pinMode(pirSensor, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int pirpin = digitalRead(pirSensor);
  int sensorValue = digitalRead(pirSensor);

  if(pirpin == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}
Output :



Untuk melihat Artikel tentang Modul Arduino, silakan lihat di sini.

Semoga saja Artikel ini sangat bermanfaat bagi para IT Pemula. Selain menggunakan ESP32, kalian juga bisa menggunakan ESP8266 atau Arduino (Uno, Nano, Mega, dll) untuk Mikrokontroler yang digunakan dalam Praktikum ini. Mohon maaf apabila ada kesalahan dalam penulisan Kode. Terima Kasih 😄😘👌👍 :)

Wassalamu‘alaikum wr. wb.

Post a Comment

Previous Post Next Post