Arduino_Genuino
Published

DC Motor controls in the Arduino IoT Cloud

Control motor speed and direction of a DC Motor from the Arduino IoT Cloud dashboard

IntermediateFull instructions provided1 hour3,521
DC Motor controls in the Arduino IoT Cloud

Things used in this project

Hardware components

Arduino Nano RP2040 Connect
Arduino Nano RP2040 Connect
Or another IoT Cloud compatible board
×1
DC motor (generic)
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IoT Cloud
Arduino IoT Cloud

Story

Read more

Code

IoTDCMotor

Arduino
Upload this sketch in the Arduino IoT Cloud
/*
Sketch generated by the Arduino IoT Cloud Thing "DC Motor"
https://create.arduino.cc/cloud/things/48ac25e7-95cb-45f6-be11-10a8e679bff9
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudPercentage motorSpeed;
bool motorDirection;
bool motorSwitch;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
const int enablePin = 9;   // connected to pin 1 on the H-bridge
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// 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();
}
void loop() {
ArduinoCloud.update();
// Your code here
if (motorDirection == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
Serial.println("motor direction 1");
} else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
Serial.println("motor direction 2");
}
if (motorSwitch == 1) {
// PWM the enable pin to vary the speed
analogWrite(enablePin, motorSpeed);
Serial.println("motor on");
} else { // if the motor is not supposed to be on
//turn the motor off
analogWrite(enablePin, 0);
Serial.println("motor off");
}
}
/*
Since MotorSpeed is READ_WRITE variable, onMotorSpeedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMotorSpeedChange()  {
// Add your code here to act upon MotorSpeed change
}
/*
Since MotorDirection is READ_WRITE variable, onMotorDirectionChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMotorDirectionChange()  {
// Add your code here to act upon MotorDirection change
}
/*
Since MotorSwitch is READ_WRITE variable, onMotorSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMotorSwitchChange()  {
// Add your code here to act upon MotorSwitch change
}

Credits

Arduino_Genuino

Arduino_Genuino

91 projects • 11336 followers

Comments