All Collections
IoT Projects Tutorials
Control up to eight servo motors remotely using XinaBox xChips OC05 (Servo Driver) and CW01 (Wi-Fi Core), connected to Ubidots over HTTP
Control up to eight servo motors remotely using XinaBox xChips OC05 (Servo Driver) and CW01 (Wi-Fi Core), connected to Ubidots over HTTP

The OC05 xChip is a servo motor controller driver that uses the PCA9685 PWM controller as servo interface supported by a BU33SD5 regulator.

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

The xChip OC05 talks to CW01 using I2C protocol. OC05 has 8 PWM channels. OC05 uses PCA9685 PWM controller to control servo motors using Pulse Width Modulation. Servo motors are powered through external battery which is then regulated using BU33SD5 regulator. With CW01 attached to OC05, CW01 gets data and processes to set PWM to control position of servomotors.

By the end of this guide, you will able to control servomotors remotely using ubidots and Xinabox xChips IP01, CW01 and OC05.

Requirements

Steps-by-step

  1. Hardware Setup

  2. Setting up the Arduino IDE

  3. Create Slider Widget

  4. Coding

  5. Compile and Upload from Arduino IDE

  6. Summary

1. Hardware Setup

1. Connect CW01, OC05 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. Your servo can be connected to one of the 8 headers on the OC05.

2. Setting up the Arduino IDE

2. Install these libraries/cores 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”. To select your board from the Arduino IDE, select Tools > Board “XinaBox CW01”.

3. Create Slider 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 “OC05” 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 “degrees”:

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 "Slider" as widget type and assign the device and variable previously created.

Set minimum value to “0” and maximum to “180” degrees. Enter Step size to 1:

 

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

4. Coding

Including libraries:

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

Defining constants:

#define SERVO_MAX 450 //servomotor maximum PWM
#define SERVO_MIN 130 //servomotor minimum PWM
#define SERVO_CHANNEL 1 //Enabling only channel 1

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 = "OC05";  // Replace by your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "degrees";       // Replace by your variable label

IMPORTANT NOTE: You can choose your own Device and Variable Label, but labels of both should be assigned respectively.

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

void setup() {
 
  value = 0; //Current slider value
  prevValue = 0; //Previous slider value
  //Serial.begin(115200); //Enable it for serial debugging
  ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);//Establish Wi-Fi
 
  //connection with access point
  Wire.begin(); //Begin I2C communication
  OC05.begin(); //Begins with default I2C Addr. 0x78
  OC05.setPWMFreq(60); //Set PWM frequency to 60Hz
  //ubidots.setDebug(true); // Uncomment this line to set
}

Loop operation, keeps running and updating again and again

void loop() {
  //Getting slider value
  float value = ubidots.get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);
  //Mapping degrees(0-180) to servomotor PWM range
  value = map(value, 0, 180, SERVO_MIN, SERVO_MAX);

  // Evaluates the results obtained
  if (value != ERROR_VALUE) {
    //Checking whether slider value changed
    if (prevValue != value) {
      OC05.setPWM(SERVO_CHANNEL, value);
      prevValue = value;
    }
  }
  delay(500);
}

 The Complete Code, please read the comments:

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

#define SERVO_MAX 450 //servomotor maximum PWM
#define SERVO_MIN 130 //servomotor minimum PWM
#define SERVO_CHANNEL 1 //Enabling only channel 1

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

Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP); //Creating ubidots client
xOC05 OC05; //Creating OC05 object

float value, prevValue; //Declaring variables to store current and past //values

void setup() {
 
  value = 0; //Current slider value
  prevValue = 0; //Previous slider value
  //Serial.begin(115200); //Enable it for serial debugging
  ubidots.wifiConnection(WIFI_SSID, WIFI_PASS);//Establish Wi-Fi
 
  //connection with access point
  Wire.begin(); //Begin I2C communication
  OC05.begin(); //Begins with default I2C Addr. 0x78
  OC05.setPWMFreq(60); //Set PWM frequency to 60Hz
  //ubidots.setDebug(true); // Uncomment this line to set
}



void loop() {
  //Getting slider value
  float value = ubidots.get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);
  //Mapping degrees(0-180) to servomotor PWM range
  value = map(value, 0, 180, SERVO_MIN, SERVO_MAX);

  // Evaluates the results obtained
  if (value != ERROR_VALUE) {
    //Checking whether slider value changed
    if (prevValue != value) {
      OC05.setPWM(SERVO_CHANNEL, value);
      prevValue = value;
    }
  }
  delay(500);
}

5. Compile and Upload from Arduino IDE

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. In the Ubidots Dashboard the OC05 can be controlled with the slider widget that was added. When you slide, servomotor rotates to that specific position.

7. Summary

In this tutorial, we have shown how to control servomotors using XinaBox xChips CW01/OC05/IP01 with Ubidots remotely from anywhere. With XinaBox and Ubidots you can now control doors and windows from anywhere to automate your home. The project is fairly simple, and may take upto 15-20 minutes.

Other readers have also found useful...

Did this answer your question?