RAK17001 with RAK11310 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 begin with the Arduino setup, you need to select the RAK11310 WisBlock Core.
Figure 1: Selecting RAK11310 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>
#if defined NRF52_SERIES
#include <LoRaWan-RAK4630.h> //http://librarymanager/All#SX126x
#elif defined ARDUINO_ARCH_RP2040
#include <LoRaWan-Arduino.h>
#elif defined ESP32
#endif
#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
#define FAULT_CHECK_INTERVAL 5000 //value in [ms] ,
TimerEvent_t appTimer;
static uint32_t timers_init(void);
volatile uint16_t InterruptCount = 0;
volatile int old_time = 0;
volatile int new_time = 0;
bool TimerFlag = false;
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 Hbridege Test!");
//creat a user timer
uint32_t err_code;
err_code = timers_init();
if (err_code != 0)
{
Serial.printf("timers_init failed - %d\n", err_code);
return;
}
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.
// digitalWrite(STSPIN250_EN, LOW);
pinMode(STSPIN250_FAULT_CHECK, INPUT);
attachInterrupt(digitalPinToInterrupt(STSPIN250_FAULT_CHECK), motor_interrupt, FALLING);
TimerSetValue(&appTimer, FAULT_CHECK_INTERVAL);
TimerStart(&appTimer);
delay(100);
}
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++ )
{
analogWrite(STSPIN250_PWM, i);
delay(35);
}
delay(100);
for (uint8_t i = 255 ; i > 0 ; i-- )
{
analogWrite(STSPIN250_PWM, i);
delay(35);
}
digitalWrite(STSPIN250_PH, LOW);//Counterclockwise rotation of motor
Serial.println("The motor turns Counterclockwise ");
for (uint8_t i = 50 ; i < 255 ; i++ )
{
analogWrite(STSPIN250_PWM, i);
delay(35);
}
delay(100);
for (uint8_t i = 255 ; i > 0 ; i-- )
{
analogWrite(STSPIN250_PWM, i);
delay(35);
}
}
else
{
Serial.println("please exit the low-power mode ");
delay(1000);
}
}
}
void motor_interrupt() {
old_time = new_time;
new_time = millis();
if (new_time - old_time >= 100)
InterruptCount++;
}
/**@brief Function for handling user timerout event.
*/
void motor_check_periodic_handler(void)
{
TimerSetValue(&appTimer, FAULT_CHECK_INTERVAL);
TimerStart(&appTimer);
Serial.print("InterruptCount=");
Serial.println(InterruptCount);
if (InterruptCount >= FAULT_CHECK_COUNT)
{
Serial.print("InterruptCount1=");
Serial.println(InterruptCount);
TimerFlag = true;
}
InterruptCount = 0;
}
/**@brief Function for the Timer initialization.
@details Initializes the timer module. This creates and starts application timers.
*/
uint32_t timers_init(void)
{
TimerInit(&appTimer, motor_check_periodic_handler);
return 0;
}
If you experience any error in compiling the example sketch, check the updated code for the RAK11310 WisBlock Core Module that can be found on the RAK17001 Motor Driver Example WisBlock Repository.
- Then you can now select the right port and upload the code.
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
RAK11300 — Sample code for controlling the RAK17001 HBridge (STSPIN250): RAK17001_HBridge_STSPIN250
