Skip to main content
All CollectionsIoT Projects Tutorials
Control a Stepper Motor remotely using XinaBox xChips OC06, CW01 and Ubidots over HTTP
Control a Stepper Motor remotely using XinaBox xChips OC06, CW01 and Ubidots over HTTP

Control a Stepper Motor remotely using XinaBox xChips CW01 (ESP8266) and OC06 (PCA9554A & DRV8825) using to Ubidots over HTTP

Sergio M avatar
Written by Sergio M
Updated over 3 months ago

The ESP8266 Core and Wi-Fi module (CW01) allows users to send data from XinaBox’s modular xChips to the cloud, and control device according to the obtained data.

The xChip OC06 uses a DRV8825 Stepper Motor Controller to precisely drive and control a connected stepper motor. The DRV8825 is interfaced with I2C through a PCA9554A I/O Expander which provides all the required control signals.

By the end of this guide, you will able to control stepper motor with basic functionality to move the motor Forward or Reverse remotely using Ubidots and XinaBox xChips IP01, CW01 and OC06.

Requirements

1. Hardware Setup

Connect CW01, OC06 and IP01 together using the XC10 xBus connectors. You may connect it as shown in the diagram below. Please see this guide on how to assemble xChips generally.

Then connect your device and PC through the IP01’s USB.

2. Setting up the Arduino IDE

1. You will need to install Arduino IDE 1.8.8.

2. Install these libraries to Arduino:

NOTE: If you are not familiar with how to install libraries, please refer to the link: Installing Arduino libraries

3. With the ESP8266 platform installed, select the ESP8266 device you are working with. In the case, we are working with a “CW01 (ESP8266)”. To select your board from the Arduino IDE, select Tools > Board “XinaBox CW01”.

3. Create Switch Button Widget

1.  Login to your Ubidots account:

2.  Create a new device. To create a new device, click the "+" icon in the top right corner of the Device's section of your account.  Then, assign “OC06” as device name.

Once the device is created it will be appear listed in the device section:

3. Enter to the device created and add a new raw variable by pressing the "+" icon. The variable ought to be called “state”:

Once the variable is created you should have the following result:

Once the variable is created you should have the following result:

 IMPORTANT NOTE: In order to be able to establish the communication, the device & variable label assigned in the platform should be the same assigned in the code. To learn more about Devices & Variable Labels, refer to the following article

 

4. Go to the dashboard ("Data > Dashboards") section to create a new control widget. To create a new control widget into the Dashboard click on the “+” icon in the top-right corner of the dashboard user interface. Then, select "Switch" as widget type and assign the device and variable previously created.

Once the widget is created you should have the following result:

4. Coding

Including libraries:

#include "Ubidots.h"
#include <xOC06.h>
#include <xCore.h>

Enter your Wi-Fi Credentials and Ubidots TOKEN where indicated:

const char* UBIDOTS_TOKEN = "...";  // Put here your Ubidots TOKEN
const char* WIFI_SSID = "..."; // Put here your Wi-Fi SSID
const char* WIFI_PASS = "..."; // Put here your Wi-Fi password
const char* DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM = "OC06";  // Replace by your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "degrees";       // Replace by your variable label


IMPORTANT NOTE: Make sure that Device and Variable LABEL are respectively assigned.

One time setup, see the comments for self-explanation:

void setup() {
    //Serial.begin(74880);
    ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
    Wire.begin();
    OC06.begin();
    OC06.enable();
    //ubidots.setDebug(true); // Uncomment this line to set DEBUG on
}

Loop operation, keeps running and updating again and again

 void loop() {
    //Get switch state from Ubidots
    value= ubidots.get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);

    //Move motor forward or reverse
    if(value==1){
      OC06.move(400, 200, FORWARD); //(Steps,Speed,Direction)
      delay(20);
    } else {
      OC06.move(400, 200, REVERSE);
      delay(20);
    }
}

   
The complete code:

#include "Ubidots.h"
#include <xOC06.h>
#include <xCore.h>  

const char* UBIDOTS_TOKEN = "...";  // Put here your Ubidots TOKEN
const char* WIFI_SSID = "..."; // Put here your Wi-Fi SSID
const char* WIFI_PASS = "..."; // Put here your Wi-Fi password
const char* DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM = "OC05";  // Replace by your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "degrees";       // Replace by your variable label

float value; //Store value of state

xOC06 OC06; //Create OC06 object
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP); //Create Ubidots client

void setup() {
    //Serial.begin(74880);
    ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
    Wire.begin();
    OC06.begin();
    OC06.enable();
    //ubidots.setDebug(true); // Uncomment this line to set DEBUG on
}

 void loop() {
    //Get switch state from Ubidots
    value= ubidots.get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);

    //Move motor forward or reverse
    if(value==1){
      OC06.move(400, 200, FORWARD); //(Steps,Speed,Direction)
      delay(20);
    } else {
      OC06.move(400, 200, REVERSE);
      delay(20);
    }
}

5. Compile and Upload Code

1. You will now use Arduino IDE to Compile and then Upload the code to the CW01, having made sure you have selected CW01 board, and connected on the correct USB port. 

2. Go to the Ubidots Dashboard to change the state of the switch widget previously created and see the result in the OC06:

6. Summary

In this tutorial, we have learned how to control Stepper motor using CW01/OC06/IP01 with Ubidots remotely from anywhere. With XinaBox and Ubidots, you can now send 3D prints to 3D printer remotely from anywhere. The tutorial was fairly simple and can take upto 15-20 minutes.

Other readers have also found useful...


Did this answer your question?