This project builds on the tenth project in the Arduino Starter Kit, the Zoetrope. The project has the same functionality as the Starter Kit project, but moved the control panel to the Cloud. If you have a Starter Kit, you will have all the components you need except of the 9V Battery.In this project we will hook our Arduino up to a DC Motor and build a dashboard in the Arduino IoT Cloud that controls the direction and the speed of the motor. We will also be able to toggle the motor on and off from the dashboard.
You will need an Arduino board that is IoT Cloud compatible, we are using the Nano RP2040 Connect.
Step 1: WiringWe start by connecting our components to the breadboard. The H-bridge acts as a motor driver and has two rows of pins, and you will want to keep them separate from each other, so when you are placing the H-bridge, make sure to place it in the middle of the breadboard. You can see that there is a small notch on one end of the H-bridge, use that to get the right orientation. From now on, I will refer to the pins on this component as bottom or top, 1-8 going left to right.
First you need to place the Arduino on the breadboard and push the H-bridge on the breadboard. Then, use jumper wires to connect 5V and GND on the Arduino to one of the vertical rails along the side of the breadboard.
Now connect the 9V Battery to the other vertical rail, then connect the GND of the battery with the Arduino's GND via the breadboard.
Finally, you can connect the H-bridge as the following:
- Top-1 pin on the H-bridge to the 5V Arduino pin
- Bottom-1 to D9
- Bottom-2 to D3
- Bottom-7 to D2
- Bottom-3 to DC Motor ground wire
- Bottom-6 to the DC Motor power.
- The H-bridge bottom-4 and bottom-5 pins to GND
When complete, your circuit should look like this:
To get started with this step, you will need some very basic knowledge of the Arduino IoT Cloud service. If you have built any previous project using the service, then don’t worry, you know all that you need to know.
If you are new to the Arduino IoT Cloud, then take some time to read through the Getting Started Guide and you will be good to go. There are also a bunch of tutorials available
In the cloud, you need to create a new Thing, and configure your device.
You then add three variables:
- Boolean Variable to toggle the motor on and off, named “motorSwitch”
- Boolean Variable to switch the direction of the motor, named “motorDirection”
- Percentage Variable to control the speed of the motor, named “motorSpeed”
Now, you will need to build a dashboard to create the controllers. Go to the dashboards section, and create a new dashboard.
Inside, create three widgets:
- Two “switch” widgets linked to the motorSwitch & motorDirection Variables
- One “slider” widget with a value range of 0-255, linked to the motorSpeed Variable
For now, you can press the buttons and change the slider but they won’t perform any actions since we haven’t uploaded the sketch to the board yet.
Step 3: CodeFor this project, the majority of the code is made up of simple if statements, and those are all contained in the loop function. We use two pins on the Arduino to control the direction of the motor, and one to toggle it on and off. If you want to skip ahead, you will find the code in its entirety at the bottom of the page.
Let's get started by defining the pins we’ll be using. Do this by navigating to Thing -> Code tab, and then add the following lines in the program below the inclusion of the `thingProperties.h` file.
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
Then we need to set these pins as outputs, add the following lines of code into the setup function
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
Now, let’s get to the conditionals, we will need one to set the direction. It will check the motorDirection variable and set the control pins accordingly. Add this block of code in the loop.
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");
}
The other conditional statement will check if the motor is supposed to be on or not. If it is, the enablePin will be set to whatever value the motorSpeed variable is reading, otherwise it will be set to 0, meaning that the motor is turned off. Do all this by adding the following block of code in the loop function, directly below the previous addition.
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");
}
The full code is available below at the end of the page.
ConclusionOnce you have uploaded the sketch, you should be able to control the motor from the dashboard you created.
Comments