RAK19006 With RAK11310 WisBlock Core Guide
- First, you need to select the RAK11310 WisBlock Core.
Figure 1: Selecting RAK11310 as WisBlock Core- Next, copy the following sample code into your Arduino IDE:
Click to view the code
/**
* @file RAK19006_WirelessCharger.ino
* @author rakwireless.com
* @brief show the wireless charger power voltage by the oled
* @version 0.1
* @date 2021-02-23
* @copyright Copyright (c) 2020
* @note platform RAK11300
**/
#include <Wire.h>
#include <U8g2lib.h> //Click here to get the library: http://librarymanager/All#u8g2
#define LED1 23
#define LED2 24
#define PIN_VBAT WB_A0 //Analog pin to read battery level definition
/** Millivolt per LSB constant value = 3.3V ADC range and 12-bit ADC resolution = 3300mV/4096 */
#define VBAT_MV_PER_LSB (0.806F)
/** Voltage divider constant = 1.5M + 1M voltage divider on VBAT = (1.5M / (1M + 1.5M)) */
#define VBAT_DIVIDER (0.6F)
/** Compensation factor for the VBAT divider */
#define VBAT_DIVIDER_COMP (1.852)
/** Formula to calculate real battery voltage */
#define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB)
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
float g_powerVoltage = 0;
void oled_show();
void readVBAT_init();
void get_BATvoltage();
void led1_on();
void led1_off();
void led2_on();
void led2_off();
void setup()
{
time_t timeout = millis();
readVBAT_init();
u8g2.begin();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
void loop()
{
led1_on();
led2_off();
get_BATvoltage();
oled_show();
delay(200);
led2_on();
led1_off();
delay(200);
}
void led1_on()
{
digitalWrite(LED1, HIGH);
}
void led1_off()
{
digitalWrite(LED1, LOW);
}
void led2_on()
{
digitalWrite(LED2, HIGH);
}
void led2_off()
{
digitalWrite(LED2, LOW);
}
void oled_show()
{
char data[32] = {0};
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB10_tr); // choose a suitable font
u8g2.drawStr(3, 15, "Power voltage:");
memset(data, 0, sizeof(data));
sprintf(data, "%3.2f V",g_powerVoltage);
u8g2.drawStr(3, 45, data);
u8g2.sendBuffer(); // transfer internal memory to the display
}
void readVBAT_init()
{
// Set the resolution to 12-bit (0..4095)
analogReadResolution(12); // Can be 8, 10, 12 or 14
// Let the ADC settle
delay(10);
}
void get_BATvoltage()
{
unsigned int sum = 0,average_value = 0;
unsigned int read_temp[10] = {0};
unsigned char i = 0;
unsigned int adc_max = 0;
unsigned int adc_min = 4095;
average_value = analogRead(PIN_VBAT);
for(i=0;i<10;i++)
{
read_temp[i] = analogRead(PIN_VBAT);
if(read_temp[i] < adc_min)
{
adc_min = read_temp[i];
}
if(read_temp[i] > adc_max)
{
adc_max = read_temp[i];
}
sum = sum + read_temp[i];
// Serial.println(read_temp[i]);
// delay(1);
}
average_value = (sum - adc_max - adc_min) >> 3;
Serial.printf("The ADC value is:%d\r\n",average_value);
// Serial.println(average_value);
g_powerVoltage = average_value * REAL_VBAT_MV_PER_LSB * 0.001;
Serial.print("The battery voltage is:");
Serial.print(g_powerVoltage,2);
Serial.println(" V");
}
NOTE
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 RAK19006 WisBlock Example Code Repository.
- Once the example code is open, install the U8g2 library by clicking the yellow highlighted link.
Figure 1: Accessing the library used for RAK19006 Module
Figure 1: Installing the compatible library for RAK19006 Module- Then you can now select the right port and upload the code.
Figure 1: Selecting the correct Serial Port
Figure 1: Uploading the RAK19006 Sample code- When you successfully uploaded the example sketch, open the Serial Monitor of the Arduino IDE to see the RAK19006 reading logs. You will be able to see the battery voltage both on the Serial Monitor and OLED Display.
Figure 1: RAK19006 Reading logs