I have a 4-20mA sensor connected to a current to voltage converter and then to the analog pin. It always returns 1023 unless I also plug the arduino into an usb port on my pc, in which case it reads correctly. Any ideas why this might be?
USB ports on PCs are often grounded to mains ground so you might have inadvertently created a ground loop. Does it happen when connect to a laptop running on battery power, without the mains connected?
Could you share your connection diagram?
Will do both suggestions once I'm back on Monday. Thank you!
Andym535:
USB ports on PCs are often grounded to mains ground so you might have inadvertently created a ground loop. Does it happen when connect to a laptop running on battery power, without the mains connected?
Yes it runs correctly even when connected to a laptop running on battery.
alesam:
Could you share your connection diagram?
Also here is the main of my code:
#include <Arduino.h>
#include "Display.h"
#define MINIMUM_DEWPOINT -100
#define MAXIMUM_DEWPOINT 20
#define AVG_READING_SIZE 20
float dewPoint;
uint8_t recordIndex = 0;
uint16_t analogReadingValue[20];
int sensorMin = 0;
int sensorMax = 1023;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
Display::init();
for(uint8_t i = 0;i<AVG_READING_SIZE;i++)
{
analogReadingValue[i] = 0;
}
}
uint16_t getAvg(uint16_t *arr,uint8_t numberOfElements)
{
uint32_t sum = 0;
for (uint8_t i = 0 ; i < numberOfElements ; i++)
{
sum += arr[i];
}
return (uint16_t)(sum / numberOfElements);
}
void loop() {
analogReadingValue[recordIndex] = analogRead(A0);
dewPoint = map(getAvg(analogReadingValue,AVG_READING_SIZE), sensorMin, sensorMax, MINIMUM_DEWPOINT, MAXIMUM_DEWPOINT);
recordIndex++;
if(recordIndex==AVG_READING_SIZE)
{
recordIndex = 0;
}
Display::displayDewpoint(dewPoint);
Serial.println(dewPoint);
Serial.println(getAvg(analogReadingValue, AVG_READING_SIZE));
delay(2000);
}
Have you got details of the sensor? For a current loop sensor you could just measure the voltage drop across a 250 Ohm resistor in series with the output instead of using the I to V converter.
Andym535:
Have you got details of the sensor? For a current loop sensor you could just measure the voltage drop across a 250 Ohm resistor in series with the output instead of using the I to V converter.
It is much easier for me to use the converter in the implementaion. However, I managed to solve it. Apparently 5V reference on the 5V pin was the issue as 5V was only supplied to the Vin pin and it was referencing to 0V from the regular 5V having nothing plugged in. Swapping the power supply from Vin to 5V solved the issue