1. Home
  2. Docs
  3. VSDSquadron Mini DataShee...
  4. Projects
  5. Home Automation Using Arduino IOT Cloud

Home Automation Using Arduino IOT Cloud

  • CH32V003X
  • ESP32
  • Servo Motors (SG90)
  • Jumper Wires

Pinout Diagram for Smart Door:

Table for Pin connection:

 Servo Motor (SG90)CCH32V003x
VCC (Red Wire)VIN
PWM (Blue Wire)PD4
GND (Black Wire)GND
 ESP32CH32V000x
D4PD2
#defineSERVO_PIN PD4
#defineINPUT_PIN PD2

voidsetup()
{
  pinMode(SERVO_PIN, OUTPUT);
  pinMode(INPUT_PIN, INPUT);
  digitalWrite(SERVO_PIN, LOW); // Set the initial position of the servo to 90 degrees
}
voidloop()
{
  staticboolprevInputState = LOW; // Variable to store the previous state of the input pin
  
  // Read the current state of the input pin
  boolcurrentInputState = digitalRead(INPUT_PIN);
  
  // Check if the input pin transitioned from LOW to HIGH
  if(currentInputState == HIGH &&prevInputState == LOW){
    // Sweep the servo from 90 to 150 degrees
    for(int angle = 90; angle <= 150; angle++){
      intpulseWidth = map(angle, 0, 180, 500, 2500); // Map the angle to the PWM pulse width
      digitalWrite(SERVO_PIN, HIGH);
      delayMicroseconds(pulseWidth);
      digitalWrite(SERVO_PIN, LOW);
      delay(15);
    }
    
    // Return the servo to the initial position (90 degrees)
    digitalWrite(SERVO_PIN, HIGH);
    delayMicroseconds(1500); // Set the pulse width for 90 degrees
    digitalWrite(SERVO_PIN, LOW);
    delay(15);
  }
  
  // Check if the input pin transitioned from HIGH to LOW
  if(currentInputState == LOW &&prevInputState == HIGH){
    // Sweep the servo from 90 to 30 degrees
    for(int angle = 90; angle >= 10; angle--){
      intpulseWidth = map(angle, 0, 180, 500, 2500); // Map the angle to the PWM pulse width
      digitalWrite(SERVO_PIN, HIGH);
      delayMicroseconds(pulseWidth);
      digitalWrite(SERVO_PIN, LOW);
      delay(15);
    }
    
    // Return the servo to the initial position (90 degrees)
    digitalWrite(SERVO_PIN, HIGH);
    delayMicroseconds(1500); // Set the pulse width for 90 degrees
    digitalWrite(SERVO_PIN, LOW);
    delay(15);
  }

The below code is for setting up the ESP32 for it to work via wifi. This code block is for setting up the required header files for creating the necessary parameters to setup the Arduino “thing”.

#include<ArduinoIoTCloud.h>
#include<Arduino_ConnectionHandler.h>

constchar DEVICE_LOGIN_NAME[]  = ;

constcharSSID[]               = ;    // Network SSID (name)
constcharPASS[]               = ;    // Network password (use for WPA, or use as key for WEP)
constchar DEVICE_KEY[]  = ;    // Secret device password

voidonButtonChange();

bool button;

voidinitProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(button, READWRITE, ON_CHANGE, onButtonChange);

}

WiFiConnectionHandlerArduinoIoTPreferredConnection(SSID, PASS);

The “thingProperties” contains all the necessary parameters to setup the system to work via cloud like the device name, SSID for the network you are using to connect through Wi-Fi, the network password and the secret key that is created when setting up the Arduino “Thing”. The setup function configures the pin 4 as output which will be connected to the VSDsquadron mini. The baud rate is set to 9600. The onButtonChange function looks for the state of the button variable which is virtual pin created using Arduino “THING”. If we get high on, it sets the output pin 4 as high.

#include"thingProperties.h"

voidsetup(){
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

voidloop(){
  ArduinoCloud.update();
  // Your code here 
  
  
}

/*
  Since Button is READ_WRITE variable, onButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
voidonButtonChange()  {
  // Add your code here to act upon Button change
  if(button == 1){
    digitalWrite(4,HIGH);
  }
  else{
    digitalWrite(4,LOW);
  }
}

#defineSERVO_PIN PD4
#defineINPUT_PIN PD2

voidsetup(){
  pinMode(SERVO_PIN, OUTPUT);
  pinMode(INPUT_PIN, INPUT);
  digitalWrite(SERVO_PIN, LOW); // Set the initial position of the servo to 90 degrees
}

voidloop(){
  staticboolprevInputState = LOW; // Variable to store the previous state of the input pin
  
  // Read the current state of the input pin
  boolcurrentInputState = digitalRead(INPUT_PIN);
  
  // Check if the input pin transitioned from LOW to HIGH
  if(currentInputState == HIGH &&prevInputState == LOW){
    // Sweep the servo from 90 to 150 degrees
    for(int angle = 90; angle <= 150; angle++){
      intpulseWidth = map(angle, 0, 180, 500, 2500); // Map the angle to the PWM pulse width
      digitalWrite(SERVO_PIN, HIGH);
      delayMicroseconds(pulseWidth);
      digitalWrite(SERVO_PIN, LOW);
      delay(15);
    }
    
    // Return the servo to the initial position (90 degrees)
    digitalWrite(SERVO_PIN, HIGH);
    delayMicroseconds(1500); // Set the pulse width for 90 degrees
    digitalWrite(SERVO_PIN, LOW);
    delay(15);
  }
  
  // Check if the input pin transitioned from HIGH to LOW
  if(currentInputState == LOW &&prevInputState == HIGH){
    // Sweep the servo from 90 to 30 degrees
    for(int angle = 90; angle >= 10; angle--){
      intpulseWidth = map(angle, 0, 180, 500, 2500); // Map the angle to the PWM pulse width
      digitalWrite(SERVO_PIN, HIGH);
      delayMicroseconds(pulseWidth);
      digitalWrite(SERVO_PIN, LOW);
      delay(15);
    }
    // Return the servo to the initial position (90 degrees)
    digitalWrite(SERVO_PIN, HIGH);
    delayMicroseconds(1500); // Set the pulse width for 90 degrees
    digitalWrite(SERVO_PIN, LOW);
    delay(15);
  }
   // Update the previous input state
  prevInputState = currentInputState;
}
#include"thingProperties.h"

voidsetup(){
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

voidloop(){
  ArduinoCloud.update();
  // Your code here 
  
 }
/*
  Since Button is READ_WRITE variable, onButtonChange() is
  executed every time a new value is received from IoT Cloud.
*/
voidonButtonChange()  {
  // Add your code here to act upon Button change
  if(button == 1){
    digitalWrite(4,HIGH);
  }
  else{
    digitalWrite(4,LOW);
  }
}
	

Here is the demonstration of the application