Not able to control micro servo with RAK17001 + RAK4631 + RUI3

Hi,

I want to control a micro servo (SG90) with the RUI3 BSP. I’ve been able to control the servo with the Arduino BSP but not with RUI3.

My setup:

  • RAK4631 + RAK19007
  • RAK17001 WisBlock H-Bridge Module
  • Sg90 Servo
  • 18650 Li Ion Battery
  • VisualStudio

In the RAK17001 I have set a header jumper and shorted VM and BAT. The VCC and GND terminals of the servo are connected directly to the battery and the Signal terminal of the servo is connected to A1 in the RAK17001.

This setup moves the servo without problems using Servo.h and the Arduino BSP, attaching the servo to pin WB_IO3. I’ve not been able to make it work with RUI3. Before posting all the things I have tried, I wanted to ask quickly if this setup is correct. Any help will be greatly appreciated.

What code are you using?

Hi Bernd,

Here is the code that is working with Arduino BSP:

/**
   @file Sweep_simple.ino
**/

#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

#include <Servo.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

Servo myservo;  // create servo object to control a servo


int pos = 0;    // variable to store the servo position

void setup()
{
  Serial.begin(115200);
  time_t timeout = millis();
  while (!Serial)
  {
    if ((millis() - timeout) < 5000)
    {
      delay(100);
    }
    else
    {
      break;
    }
  }
  Serial.println("RAK17001 Hbridege Test!");

  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);

  myservo.attach(STSPIN250_PWM);

  delay(500);
}

void loop()
{
  for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);              
    delay(15);                      
  }

  delay(1000);

  for (pos = 180; pos >= 0; pos -= 1) { 
    myservo.write(pos);            
    delay(15);                    
  }
  
  delay(1000);
}

I’ve stripped the RAK17001_HBridge_STSPIN250.ino example to it’s minimum and added the Servo.h library, the libraries that are loaded are:

SX126x-Arduino 2.0.29 C:\Users\User\Documents\Arduino\libraries\SX126x-Arduino
SPI 1.0 C:\Users\ User\AppData\Local\Arduino15\packages\rakwireless\hardware\nrf52\1.3.3\libraries\SPI
Servo 1.1.2 C:\Users\ User\AppData\Local\Arduino15\packages\rakwireless\hardware\nrf52\1.3.3\libraries\Servo
Adafruit TinyUSB Library 1.7.0 C:\Users\User\AppData\Local\Arduino15\packages\rakwireless\hardware\nrf52\1.3.3\libraries\Adafruit_TinyUSB_Arduino

If I then change the board in VisualStudio to WisBlock Core RAK4631 and try to compile the same sketch the first error that appears is:

#error "SX126x-Arduino V2.0 does support all LoRaWAN regions without definition of 'REGION_XXYYY.

And the second is:

fatal error: Servo.h: No such file or directory

The first one is easy to overcome if I remove the #include <LoRaWan-RAK4630.h>. I’ve not been able to overcome the second error.

I’ve tried installing the Arduino Servo library, and I get the error:

C:\Users\User\Documents\Arduino\libraries\Servo\src\nrf52\Servo.cpp:31:24: error: 'NRF_PWM_CHANNEL_COUNT' was not declared in this scope

And many more errors that come after that one.

I’ve also tried with NRF52_ISR_Servo library. If I try to compile the NRF52_MultipleServos.ino sketch the error that appears is:

C:\Users\User\Documents\Arduino\libraries\NRF52_ISR_Servo\src/NRF52_ISR_Servo.hpp:50:10: fatal error: Adafruit_TinyUSB.h: No such file or directory

If I manually add the Adafruit_TinyUSB library into my libraries folder the error that appears is then:

C:\Users\User\Documents\Arduino\libraries\Adafruit_TinyUSB_Library\src/tusb_config.h:47:4: error: #error TinyUSB Arduino Library does not support your core yet

I have many sketches with boards running RUI3 so I’m confident that my environment is ok. Do you see anything that I’m doing wrong?

RUI3 has it’s own LoRa library built in.

SX126x-Arduino library is not required and is not compatible with RUI3

Adafruit_TinyUSB_Library is only for the Arduino nRF52 BSP. It is not required and not compatible with RUi3.

C:\Users\User\Documents\Arduino\libraries\Servo\src\nrf52\Servo.cpp:31:24: error: 'NRF_PWM_CHANNEL_COUNT' was not declared in this scope

Might be a dependency of the Servo Library that is not available in RUI3.
Try to set it as a #define NRF_PWM_CHANNEL_COUNT 4, this is the setting in the nRF52 BSP.

Here is a sample code for RUI3 and RAK4631.

I can’t test it, I don’t have a DC motor available, but it compiles without problems.
RUI3-RAK17001.ino (3.9 KB)

Thanks for your efforts.

In your first possible solution I added the #define NRF_PWM_CHANNEL_COUNT 4 with the Arduino Servo.h library installed, I still have many errors, I attach a file with them.
Servo.h and Rui3 errors.log (17.8 KB)

I tried your RUI3-RAK17001.ino example code. When I upload that code I get these lines all the time:

InterruptCount=0
please exit the low-power mode 
please exit the low-power mode 
please exit the low-power mode 
please exit the low-power mode 
please exit the low-power mode 
InterruptCount=0
please exit the low-power mode 
please exit the low-power mode 
please exit the low-power mode 
please exit the low-power mode

I’ve modified the code so it doesn’t check for low power mode, it still continues to compile but the module freezes after the first analogWrite(), it freezes with or without the motor connected to the RAK17001 so to test this out you don’t need a motor, at least it’s freezing my 4631’s (RUI_4.2.1_RAK4631). I’ve added some lines that write the position on each iteration of the for loops, this is what I get on the serial port:

RAKwireless RUI3 Node
------------------------------------------------------
Setup the device with WisToolBox or AT commands before using it
Version RUI3-RAK17001-V1.0.0
------------------------------------------------------
Current Work Mode: LoRa P2P.
The motor turns clockwise
50

Any more ideas I can try?

No other ideas until I have a motor to test myself.
Should have one latest beginning of next week.

Found there is a problem with analogWrite.
Not sure why. Forwarded problem to R&D team.

That’s great to hear. A couple of weeks ago I encountered an issue maybe related or maybe not to this one. It was with the RAK18001 buzzer module. With Arduino BSP it was working as it’s supposed to in all slots, but with RUI3 the RAK4631 was freezing after the tone() function. If you want details, I can post the code I was using.

Downgrade your installed BSP to RUI3 V4.1.1
RAK17001 PWM works with that version.

I tried to downgrade the module to RUI3 4.1.1 but there is something I’m missing. I’ve flashed the RAK4631 with the RUI_4.1.1_RAK4631_final.hex file found inside this zip file with my RAKDAP1 and pyocd. I then print the RUI3 version with the AT command and it prints RUI_4.1.1_RAK4631. I then flash the sketch with VisualStudio and when I check again the RUI3 version it says RUI_4.2.1_RAK4631. The module is freezing as before in the first analogWrite. Do I also have to change anything in VisualStudio to downgrade the RUI3 version?

Disregard the last message, I figured out that I also had to change the version in the Boards Manager of the Arduino IDE of the RAKwireless RUI nRF Boards package, then delete the .vscode and build folders from the project and select again the board in VisualStudio.

With that the servo moves as is supposed to with the RAK17001_HBridge_STSPIN250.ino example.

The RAK18001 Buzzer is also working with this RUI3 version.

Thank you very much for your help in solving this issue.

1 Like

Glad to hear its solved (for now).
Ticket to fix in RUI3 is submitted to R&D.

Added an example for controlling a DC motor attached to the RAK17001 through LoRaWAN or LoRa P2P to our RUI3-Best-Practice==> RUI3-RAK17001-DC-Motor

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.