RAK17000 With RAK11200 WisBlock Core Guide
- First, 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 RAK17000 WisBlock example repository which controls two DC motors.
Click to view the code
/**
@file RAK17000_Motor_Driver_DRV8833.ino
@author rakwireless.com
@brief Control the two motors for forward and reverse rotation from slow to fast and then from fast to slow.
@version 0.1
@date 2021-5-14
@copyright Copyright (c) 2020
**/
#include <Arduino.h>
#define MOTOR_AIN1 SCK
#define MOTOR_AIN2 SS
#define MOTOR_BIN1 WB_IO3
#define MOTOR_BIN2 MOSI
#define MOTOR_SLEEP WB_IO6
// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
#define LEDC_CHANNEL_1 1
#define LEDC_CHANNEL_2 2
#define LEDC_CHANNEL_3 3
// use 13 bit precision 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()
{
// Enable DRV8833 output.
pinMode(MOTOR_SLEEP, OUTPUT);
digitalWrite(MOTOR_SLEEP, HIGH);
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT); // Set channel 0
ledcSetup(LEDC_CHANNEL_1, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT); // Set channel 1
ledcSetup(LEDC_CHANNEL_2, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT); // Set channel 2
ledcSetup(LEDC_CHANNEL_3, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT); // Set channel 3
ledcAttachPin(MOTOR_AIN1, LEDC_CHANNEL_0);
ledcAttachPin(MOTOR_AIN2, LEDC_CHANNEL_1);
ledcAttachPin(MOTOR_BIN1, LEDC_CHANNEL_2);
ledcAttachPin(MOTOR_BIN2, LEDC_CHANNEL_3);
}
void loop()
{
ledcAnalogWrite(LEDC_CHANNEL_1, 0);
ledcAnalogWrite(LEDC_CHANNEL_3, 0);
for(uint8_t i = 0 ;i < 255 ;i++ )
{
ledcAnalogWrite(LEDC_CHANNEL_0, i);
ledcAnalogWrite(LEDC_CHANNEL_2, i);
delay(50);
}
delay(100);
for(uint8_t i = 255 ;i > 0 ;i-- )
{
ledcAnalogWrite(LEDC_CHANNEL_0, i);
ledcAnalogWrite(LEDC_CHANNEL_2, i);
delay(50);
}
ledcAnalogWrite(LEDC_CHANNEL_0, 0);
ledcAnalogWrite(LEDC_CHANNEL_2, 0);
for(uint8_t i = 0 ;i < 255 ;i++ )
{
ledcAnalogWrite(LEDC_CHANNEL_1, i);
ledcAnalogWrite(LEDC_CHANNEL_3, i);
delay(50);
}
delay(100);
for(uint8_t i = 255 ;i > 0 ;i-- )
{
ledcAnalogWrite(LEDC_CHANNEL_1, i);
ledcAnalogWrite(LEDC_CHANNEL_3, i);
delay(50);
}
delay(100);
}
If you experience any error in compiling the example sketch, check the updated code for the RAK11200 WisBlock Core Module that can be found on the RAK17000 Motor Driver Example WisBlock Repository.
The basic example code for stepper motor control can be found on the RAK17000 WisBlock Example Code repository as well.
- 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 RAK17000 Sample code- When you successfully uploaded the example sketch, you'll now see that the motor will start spinning and reversing direction repeatedly.
Resources
For WisBlock Core RAK11200:
-
Sample Code: DC Motor Control — RAK17000_Motor_Driver_DRV8833
-
Sample Code: Stepper Motor Control — RAK17000_Stepper_Driver_DRV8833
