-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathRAK5811_0-5V.ino
69 lines (59 loc) · 1.58 KB
/
RAK5811_0-5V.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @file RAK5811_0-5V.ino
* @author rakwireless.com
* @brief 0 to 5V analog input example.
* @version 0.1
* @date 2020-12-18
*
* @copyright Copyright (c) 2020
*/
#include <Arduino.h>
#define NO_OF_SAMPLES 128 //Multisampling
void setup()
{
/* WisBLOCK 5811 Power On*/
pinMode(WB_IO1, OUTPUT);
digitalWrite(WB_IO1, HIGH);
/* WisBLOCK 5811 Power On*/
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
adcAttachPin(WB_A1); //Connect pin to ADC peripherals
analogSetAttenuation(ADC_11db);
analogReadResolution(12);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
}
void loop()
{
int i;
int sensor_pin = WB_A1; // the input pin A1 for the potentiometer
int adc_raw = 0;
int average_adc_raw;
float voltage_mcu_ain;
float voltage_sensor; // variable to store the value coming from the sensor
for (i = 0; i < NO_OF_SAMPLES; i++)
{
adc_raw += analogRead(sensor_pin);
}
average_adc_raw = adc_raw / NO_OF_SAMPLES;
/* Convert adc_raw to voltage in mV
* Func esp_adc_cal_raw_to_voltage only for attenuation == ADC_11db and sample bits == 12
*/
voltage_mcu_ain = esp_adc_cal_raw_to_voltage(average_adc_raw);
voltage_sensor = voltage_mcu_ain / 0.6; //WisBlock RAK5811 (0 ~ 5V). Input signal reduced to 6/10 and output
Serial.printf("-------average_value------ = %d\n", average_adc_raw);
Serial.printf("-------voltage_sensor------ = %f\n", voltage_sensor);
delay(2000);
}