Hello, so im in the middle of a personal project where i need to meassure 1 to 4 4-20ma pressure sensors (Wika A10 0-6 bar). Since everything will be powered from a farm tractor which has voltage variations im using a LM2596 Step Down circuit to regulate the voltage to flat 9v and power the arduino and a XL6009 Step Up circuit to go back up to flat 12v and power the sensors. Im using an Arduino nano with a 20x4 LCD display with an I2C module.
My problem is that when i connect the sensors and read the analog pins the ADC values are around 150 to 200 for each connected sensor, im wondering if i can get that value lower and/or why this happans.
Here is the code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BUTTON_PIN 3
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int NUM_SENSORS = 4;
const int SENSOR_PINS[NUM_SENSORS] = {A0, A1, A2, A3};
const int NUM_MEASUREMENTS = 100;
const float MAX_PRESSURE = 6.0;
volatile bool checkCalibration = false;
int conSensors;
float sensorsADC[NUM_SENSORS];
int sensorData[NUM_SENSORS][NUM_MEASUREMENTS];
float medianData[NUM_SENSORS];
float previousPressureValues[NUM_SENSORS];
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.clear();
lcd.backlight();
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), calibrationCall, RISING);
conSensors = 4;//checkConnectedSensors();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connected Sensors: ");
lcd.print(conSensors);
delay(3000);
lcd.clear();
}
void loop() {
if (checkCalibration) {
checkCalibration = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CALIBRATING");
delay(1000);
for (int i = 0; i < conSensors; i++) {
calibrateSensor(i);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Done!");
delay(1000);
lcd.clear();
displaySensorValues();
}
for (int j = 0; j < conSensors; j++) {
for (int i = 0; i < NUM_MEASUREMENTS; i++) {
sensorData[j][i] = analogRead(SENSOR_PINS[j]);
}
delayMicroseconds(20);
}
for (int i = 0; i < conSensors; i++) {
quicksort(sensorData[i], 0, NUM_MEASUREMENTS - 1);
medianData[i] = sensorData[i][NUM_MEASUREMENTS / 2];
Serial.print("ADC S");
Serial.print(i);
Serial.print("= ");
Serial.println(medianData[i]);
float pressureValue = map(medianData[i], sensorsADC[i], 1012, 0, MAX_PRESSURE);
pressureValue = constrain(pressureValue, 0, MAX_PRESSURE);
if (pressureValue != previousPressureValues[i]) {
previousPressureValues[i] = pressureValue;
lcd.setCursor(0, i);
lcd.print("Sensor ");
lcd.print(i + 1);
lcd.print(": ");
lcd.print(pressureValue, 2);
lcd.print(" bar");
}
}
}
void calibrationCall() {
checkCalibration = true;
}
void calibrateSensor(int sensorIndex) {
float sensorSum = 0.0;
for (int i = 0; i < NUM_MEASUREMENTS; i++) {
sensorSum += analogRead(SENSOR_PINS[sensorIndex]);
delayMicroseconds(20);
}
sensorsADC[sensorIndex] = sensorSum / (float)NUM_MEASUREMENTS;
}
int checkConnectedSensors() {
int temp = 0;
for (int i = 0; i < NUM_SENSORS; i++) {
int j = analogRead(SENSOR_PINS[i]);
if (j < 200) {
temp++;
}
}
return temp;
}
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quicksort(arr, low, pivotIndex - 1);
quicksort(arr, pivotIndex + 1, high);
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
void displaySensorValues() {
for (int i = 0; i < conSensors; i++) {
lcd.setCursor(0, i);
lcd.print("Sensor ");
lcd.print(i + 1);
lcd.print(": ");
lcd.print(previousPressureValues[i], 2);
lcd.print(" bar");
}
}
Here is the circuit diagram:
I only draw 1 sensor but each of the 4 possible sensors has its own 4.7kOhm resistor to its analog pin and its own 250Ohm resistor to ground.