Arduino can read this circuit

Hi,
I am test the circuit from:

I used a Arduino instead of a Nano, and input a 12Vdc and the unit can read any resistance, why?
Thanks
Adam

#include <Adafruit_SSD1306.h>
#include <Wire.h> // Wire Library for OLED
#define SCREEN_WIDATA_PINH 128 // OLED display Width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define LM317_REF 1.25
#define LM317_Resistance 10.1
const int numReadings = 50; // used for averaging / we will take 50 samples and average it to get ADC value 
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int ADCaverage = 0;             // the average
float R;
int inputPin = A0;
Adafruit_SSD1306 display(SCREEN_WIDATA_PINH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.setTextColor(WHITE); // set LCD Colour
  display.setRotation(2); // it has modes 1,2,3,4
  //
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
delay(500);
}
void loop() {
  total = total - readings[readIndex];  // subtract the last reading:
  readings[readIndex] = analogRead(inputPin); // read from the sensor:
  total = total + readings[readIndex];  // add the reading to the total:
  readIndex = readIndex + 1; // advance to the next position in the array:
  if (readIndex >= numReadings) {
    // if we're at the end of the array...
    // ...wrap around to the beginning:
    readIndex = 0;
  }
  ADCaverage = total / numReadings; // calculate the average:
  Serial.print("AVG: ");
  Serial.print(ADCaverage);
  float voltage = ADCaverage * (5.0 / 1024.0); // Convert ADCaverage t0 Voltage
  Serial.print(" \t \t"); // give me a little TAb would ya
  Serial.print(voltage, 3); // Print Voltage to Serial Monitor
  Serial.print(" \t \t");// give me another little TAb would ya
  R = voltage / (LM317_REF / LM317_Resistance) ;
  Serial.print("Resistance: ");
  Serial.println(R);
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(10, 10);
  display.print(R,3);
  display.print(" R");
  display.display();
  delay(50);
}

Do you have a question?
Any observations?
Summing 50 10-bit numbers into a 16 bit int is a little optimistic

They're already zero - waste of code space

1 Like

Thanks.
The video shown a reading by a small R tested.
My unit shown a number of 2.792 for whatever resistance connected.

Have you fixed the code yet?

Thanks.
Can you help fix that please, I got kind of puzzle now.

"total" must be at least a sixteen bit unsigned type.

You haven't shown us any of the individual readings
Or a schematic

My unit shown a number of 2.792 for whatever resistance connected.

You are reading Pin A0 but you don't have anything connected to it.

2 Likes

Thanks.
Well done.
There are readings even they are not right number.

Floating pins float.

Thanks.
How to overcame? why that output just one value with any resistance?

Where did you connect A0?

1 Like

Thanks.
The original drawing and code have lot of mistakes.
I used another one works fine now.
Best.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.