forked from RAKWireless/WisBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAK5811_0-5V.ino
72 lines (60 loc) · 1.69 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
70
71
72
/**
* @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 | PULLUP);
digitalWrite(WB_IO1, HIGH);
adcAttachPin(WB_A1); //将引脚连接到ADC
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 depths; // variable to store the value of oil depths
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
depths = (voltage_sensor * 1000 - 574) * 2.5; //Convert to millivolt. 574mv is the default V output from sensor
Serial.printf("adc_raw: %d\tvoltage_mcu_ain: %f\tvoltage: %f\n", average_adc_raw, voltage_mcu_ain, voltage_sensor);
Serial.printf("depths: %d mm\n\n", depths);
delay(2000);
}