0

I have an Arduino connected to a 4-20 mA sensor through a current to voltage converter. When connected to the power supply the sensor reading fluctuates, as soon as I also connect it to a USB port it stabilizes and gives a correct reading (tested with other reader).

My main code is:

#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);
}

My circuit diagram is: enter image description here

  • 1
    Vin starts at 6 V store.arduino.cc/arduino-uno-rev3 – Juraj Mar 5 at 10:28
  • Thank you, i have swapped Vin for the 5v pin, and now it reads at a constant 1023, while retaining the same behaviour when usb connected – Adrian Paun Mar 5 at 10:48
  • If you feed 5V into Vcc (5V), you must not connect USB. ( Or vice versa :) ) – DataFiddler Mar 5 at 11:20
  • That is my goal, however as I pointed out before, the reading i get when it isn't also plugged into usb is the max value of 1023 constantly, which isn't correct – Adrian Paun Mar 5 at 11:23
  • To add to that the 2 power supplies in the diagram are connected to a switch that connects to mains through a cable – Adrian Paun Mar 5 at 11:27
0

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. Swapping the power supply from Vin to 5V solved the issue

0

You need a decoupling ceramic capacitor between A0 and GND. 100nF should work just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.