RAK14006 WisBlock Rotary Encoder Module Quick Start Guide
Prerequisite
- Assemble the RAK14006 with a WisBlock Base and WisBlock Core by following the RAK14006 Installation Guide.
- Download and install the Arduino IDE, then add the RAKwireless Core boards by installing the RAKwireless Arduino BSP.
- USB Cable
- RAK19008 WisBlock IO Extension Cable
- Li-Ion/LiPo battery (optional)
- Solar charger (optional)
Software Configuration and Example
In this example, you will be getting the rotation position of the knob or if it is pressed to your Serial Monitor.
NOTE
- This guide uses the RAK4631 WisBlock Core to demonstrate the configuration and upload process. The example is also compatible with other WisBlock Cores, including the RAK11200 and RAK11310.
- When using the RAK11200 WisBlock WiFi Module, configure the BOOT0 pin properly before uploading the firmware. Incorrect BOOT0 configuration may cause the firmware upload to fail. For more information, refer to the RAK11200 Quick Start Guide.
- You need to select first the WisBlock Core you have.
Figure 1: Selecting RAK4631 as WisBlock Core- Copy the example code below.
Click to view the code
/**
@file RAK14006_Rotary_Encoder.ino
@author rakwireless.com
@brief Rotary Encoder Example
@version 0.1
@date 2021-7-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#define ENCODER_A_PIN WB_IO6 //clockwise
#define ENCODER_B_PIN WB_IO4 //anticlockwise
#define SWITCH_PIN WB_IO5 //press key
//position is 0 when start up
long position = 0;
bool press_flag = false;
bool clockwise_flag = false;
bool anticlockwise_flag = false;
void setup(){
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
//setup our pins
pinMode(ENCODER_A_PIN, INPUT);
pinMode(ENCODER_B_PIN, INPUT);
pinMode(SWITCH_PIN, INPUT);
attachInterrupt(SWITCH_PIN, handle_1, FALLING);
attachInterrupt(ENCODER_A_PIN, handle_2, RISING);
attachInterrupt(ENCODER_B_PIN, handle_3, RISING);
//setup our serial
Serial.begin(115200);
Serial.println("You can press and twist the button!!");
}
void loop(){
if(press_flag==true)
{
press_flag = false;
Serial.println("press key");
}
if(clockwise_flag==true)
{
clockwise_flag = false;
position++;
Serial.print("position step is ");
Serial.println(position);
}
if(anticlockwise_flag==true)
{
anticlockwise_flag = false;
position--;
Serial.print("position step is ");
Serial.println(position);
}
}
void handle_1(){
press_flag = true;
}
void handle_2(){
clockwise_flag = true;
}
void handle_3(){
anticlockwise_flag = true;
}
If you experience any error in compiling the example sketch, check the updated code for the RAK14006 WisBlock Rotary Encoder Module that can be found on the RAK14006 WisBlock Example Code Repository
- Select the right serial port and upload the code.
Figure 1: Selecting the correct Serial Port
Figure 1: Uploading the sample code- When you successfully upload the sample code, you may rotate or press the knob switch of RAK14006. Then, you can open up your serial monitor to get the rotary encoder reading.
Figure 1: Rotating the knob switch of RAK14006
Figure 1: Position step reading in Serial Monitor