RAK17001 with RAK11200 WisBlock Core Guide
Prerequisite
- Assemble the RAK17001 with a WisBlock Base and WisBlock Core by following the RAK17001 Installation Guide.
- Download and install the Arduino IDE, then add the RAKwireless Core boards by installing the RAKwireless Arduino BSP.
- USB Cable
- Li-Ion/LiPo Battery (optional)
- Solar Charger (optional)
- DC Motor
Software Configuration and Example
After doing the hardware setup, this example code will work both for WisBlock battery-powered motor or an external power source. This will control the DC motor connected via terminal connector. The module will spin the motor clockwise then counterclockwise direction repeatedly.
- To start with the Arduino setup, you need to select the RAK11200 WisBlock Core.
Figure 1: Selecting RAK11200 as the WisBlock Core- Next, copy the following sample code into your Arduino IDE. This example code comes from the RAK17001 WisBlock example repository which demonstrates basic usage of RAK17001 module.
/**
@file RAK17001_HBridge_STSPIN250.ino
@author rakwireless.com
@brief Control the one motors for forward and reverse rotation from slow to fast and then from fast to slow.
@version 0.1
@date 2022-05-05
@copyright Copyright (c) 2022
**/
#include <Arduino.h>
#define STSPIN250_PH WB_IO4 //the PH pin determines the direction of the current
#define STSPIN250_PWM WB_IO3 //the PWM pin can be used to regulate the speed of the rotation
#define STSPIN250_FAULT_CHECK WB_IO5
#define STSPIN250_EN WB_IO6
#define FAULT_CHECK_COUNT 4 // if four protection interrupts detected within 5 seconds, Sensor may be thermal shutdown or Overcurrent and short-circuit protections
volatile uint16_t InterruptCount = 0;
volatile int old_time = 0;
volatile int new_time = 0;
hw_timer_t *tim1 = NULL;
bool TimerFlag = false;
// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13
// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 5000
/**
@brief Arduino like analogWrite.
*/
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255)
{
// calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(channel, duty);
}
void setup()
{
//Sensor power control
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
delay(300);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, LOW);
Serial.begin(115200);
time_t timeout = millis();
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
Serial.println("RAK17001 Test!");
tim1 = timerBegin(0, 80, true);
timerAttachInterrupt(tim1, tim1Interrupt, true);
timerAlarmWrite(tim1, 500000ul, true);
timerAlarmEnable(tim1);
pinMode(STSPIN250_PH, OUTPUT);
digitalWrite(STSPIN250_PH, HIGH);
pinMode(STSPIN250_PWM, OUTPUT);
digitalWrite(STSPIN250_PWM, LOW);
pinMode(STSPIN250_EN, OUTPUT);
digitalWrite(STSPIN250_EN, HIGH);//if set STSPIN250_EN High,sensor work normal.if set LOW, Sensors enter low power mode.
pinMode(STSPIN250_FAULT_CHECK, INPUT);
attachInterrupt(digitalPinToInterrupt(STSPIN250_FAULT_CHECK), motor_interrupt, FALLING);
delay(300);
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT); // Set channel 0
ledcAttachPin(STSPIN250_PWM, LEDC_CHANNEL_0);
}
void loop()
{
if (TimerFlag)//Thermal shutdown or Overcurrent and short-circuit protections
{
InterruptCount = 0;
digitalWrite(LED_BLUE, HIGH);
Serial.println("Sensor thermal shutdown or Overcurrent and short-circuit protections,Please check it!!!");
delay(500);
return;
}
else
{
if (digitalRead(STSPIN250_EN) == 1)
{
digitalWrite(STSPIN250_PH, HIGH); //The motor rotates clockwise
Serial.println("The motor turns clockwise");
for (uint8_t i = 50 ; i < 255 ; i++ )
{
ledcAnalogWrite(LEDC_CHANNEL_0, i);
delay(50);
}
delay(100);
for (uint8_t i = 255 ; i > 0 ; i-- )
{
ledcAnalogWrite(LEDC_CHANNEL_0, i);
delay(50);
}
digitalWrite(STSPIN250_PH, LOW);//Counterclockwise rotation of motor
Serial.println("The motor turns Counterclockwise ");
for (uint8_t i = 50 ; i < 255 ; i++ )
{
ledcAnalogWrite(LEDC_CHANNEL_0, i);
delay(50);
}
delay(100);
for (uint8_t i = 255 ; i > 0 ; i-- )
{
ledcAnalogWrite(LEDC_CHANNEL_0, i);
delay(50);
}
}
else
{
Serial.println("please exit the low-power mode ");
delay(1000);
}
}
}
void tim1Interrupt()
{
if (InterruptCount)
{
if (InterruptCount >= FAULT_CHECK_COUNT) // if three protection interrupts detected within 5 seconds, Sensor may be thermal shutdown or Overcurrent and short-circuit protections
{
TimerFlag = true;
// Serial.print("timerAlarmEnabled=");
// Serial.println(timerAlarmEnabled(tim1));
}
}
}
void motor_interrupt() {
old_time = new_time;
new_time = millis();
if (new_time - old_time >= 100)
InterruptCount++;
}
If you experience any error in compiling the example sketch, check the updated code for the RAK11200 WisBlock Core Module. You can find it on the RAK17001 Motor Driver Example WisBlock Repository.
- Then you can now select the right port and upload the code.
RAK11200 requires the Boot0 pin to be configured properly first before uploading. If not done properly, uploading the source code to RAK11200 will fail. Check the full details on the RAK11200 Quick Start Guide.
Figure 1: Selecting the correct Serial Port
Figure 1: Uploading the RAK17001 Sample code- When you have successfully uploaded the example sketch, you'll now see that the motor will start spinning and reversing direction repeatedly.
The direction is based on how the wires of DC motor is connected. It should be connected to the A1 and A2 pin of RAK17001 terminal block. You can reverse the direction by interchanging the wire connection.
Figure 1: Serial monitor logsResources
RAK11200 — Sample code for controlling the RAK17001 HBridge (STSPIN250): RAK17001_HBridge_STSPIN250
