Potentiometer analogRead - wrong drfting values!

hello everybody
I'm working on an Arduino Pro Mini clone (here)

among other things I have a potentiometer connected to A0 where I read the values for knob-like purposes. You can see the schematics in the image attached.

Everything works fine, but the analogRead(A0) gets weird values within weird range: from about 30 to about 200. Also if I don't touch the knob, the reading slowly drifts to lower values :o

Of course I checked the connections with a multimeter and the voltage readings... everything seems fine.

Any suggestions ?

Here is the code

#include <RH_ASK.h>
#include <TM1637Display.h>

// iniszializzazioni pin
#define potPin A0
#define batPin A1                                                                                                                                                                                                                                                                                                                                                                                                        
#define btnPin 2
#define txPin 13
#define pinCLK 8
#define pinDIO 9

// inizializzazione librerie RadioHead per trasmissione
RH_ASK tx(2000,1,txPin,1,true);

//inizializzazione libreria TM1637Display del display
TM1637Display digitalDisplay(pinCLK, pinDIO);
#define SEG_A   0b00000001   //      A   
#define SEG_B   0b00000010  //     ---
#define SEG_C   0b00000100  //  F |   | B
#define SEG_D   0b00001000  //     -G-
#define SEG_E   0b00010000  //  E |   | C
#define SEG_F   0b00100000  //     ---
#define SEG_G   0b01000000  //      D
//-- per scrittura “OFF” 
// ogni dgit dell’array displayOFF (quindi quale segmento è acceso di ogni digit) è definito con OR-bitwise “|” dei segmenti interessati
const uint8_t displayOFF[] = {
  0b00000000,
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_A | SEG_E | SEG_F | SEG_G,                   // F
  SEG_A | SEG_E | SEG_F | SEG_G,                   // F
  };

// inizializzazione comandi throttle
int motJuice = 0; int motJuice_perc;
int btnState; String msgStr;

// inizializzazioni partitore di tensione
double VPinRead;
double Vbatt; double Vbatt_perc;
double R1 = 23; double R2 = 1000000;


void setup() {
  Serial.begin(9600);
  pinMode(btnPin,INPUT);
  pinMode(batPin,INPUT);

  digitalDisplay.setBrightness(0x0f);  //! Sets the brightness – 0x0f =7, massima lumx;

  // SETTAGGI DI TRASMISSIONE
  if (!tx.init()){ Serial.println("init failed");}
}

void loop() {
  // ----------------------- COMANDI THROTTLE ---------------------------//
  btnState = digitalRead(btnPin);
  motJuice = analogRead(potPin);//map(analogRead(potPin),65,160,0,100);
  Serial.println(motJuice);
  
  if(btnState == LOW) // se tasto non premuto comando da throttle
  {
    motJuice = analogRead(potPin);
    motJuice = map(motJuice,0,1023,2000,1100);
    
    motJuice_perc = map(motJuice,1250,2000,0,100);
    if(motJuice_perc < 5){motJuice_perc = 0;} // filtro 
    
    digitalDisplay.showNumberDec( motJuice_perc , false); // stampa percentuale throttle
    
  } else if(motJuice_perc > 0 && btnState == HIGH) // se tasto premuto, comando -> motore spento
  {
    motJuice = 0;
    digitalDisplay.setSegments(displayOFF);
  }
  // --------------------------------------------------------------------//
  //
  // ------------------- MISURA BATTERIA con PARTITORE di TENSIONE ---------------------//
  VPinRead = map(analogRead(batPin),0,1023,3200,3850); //tra 0 e 5000 mV massima tensione del pin 
                                                    // in mV per avere + cifre significative
  Vbatt = (VPinRead/1000)*((R2/(R1+R2))) ; // diviso 1000 per riportarlo in V da mV. 
                                                     // * 3.85/3.91 - fattore di calibrazione a misura multimetro
  Vbatt_perc = map(analogRead(batPin),0,1023,0,100);
  //Serial.println(Vbatt);
  

  if(motJuice_perc == 0 && btnState == HIGH) // se throttle == 0 e bottone premuto, stampa misura batt
  {
    displayBATperc(Vbatt_perc); 
  } 
  // ----------------------------------------------------------------------------------//

  //TRASMISSIONE
  // costituzione messaggio di invio (1 digit)
  msgStr=String(motJuice); // conversione in stringa
  
  char msg[msgStr.length()+1]; //dichiarazione  char
  msgStr.toCharArray(msg,msgStr.length()+1); //conversione in char
  tx.send((uint8_t *)msg, msgStr.length()+1); // invio del messaggio 
  tx.waitPacketSent();       // attesa conclusione invio   

/*
  Serial.print("Sent:  ");Serial.print(msgStr);Serial.print(" - ");
  for (int i=0; i<= msgStr.length(); i++)
  {
   Serial.print(char(msg[i]));
  }
  Serial.println();
  */

  
  /*if(Serial.available()>0)
  {
   msg = Serial.readString();

    motJuice = msg.toInt();
  }*/
}

void displayBATperc(int Vbatt_perc)
{
  uint8_t displayMSG[] = { 0xff, 0xff, 0xff, 0xff };  // 0xff = 255 – tutto acceso

  displayMSG[0] = SEG_F | SEG_E | SEG_C | SEG_D | SEG_G;

  if(Vbatt_perc/100 == 0){displayMSG[1] = 0b00000000;}//tutto spento
  else {displayMSG[1] = digitalDisplay.encodeDigit(Vbatt_perc/100);}

  displayMSG[2] = digitalDisplay.encodeDigit(Vbatt_perc/10);
  displayMSG[3] = digitalDisplay.encodeDigit(Vbatt_perc/1); 

  digitalDisplay.setSegments(displayMSG);
}

look at using your battery voltage as the reference voltage for the ADC.
reference voltage

That's certainly a better quality schematic than we usually see in these kinds of questions. But your symptoms sound like you haven't connected the pot correctly. Check your connections against your schematic.

What is the value of the pot? Is it connected via long leads?

One side of the pot is not shown - does it go to earth/ground/0V ? ( Hope so)

Try adding a 100nF capacitor between the pot wiper and ground.

Allan

Two serious flaws shown in the schematic

  1. You do not show ground connection to the arduino you're using for uploading.

  2. You have ground connected to the pin marked GRN on the pro mini, the one next to TX/RX pins on the 6-pin header on the end. This is the pin that is supposed to be connected to the DTR pin of a serial adapter, not ground. Pin order is Gnd, Gnd, Vcc, Tx, Rx, DTR (whether you say Tx, Rx, or Rx, Tx depends which side of the connection you look at it from)... So if that's what you have it wired like, any functionality you're observing is operating purely through leakage through protection diodes (which may damage the chip over time).

(yes, it's dumb that the pro mini is marked with the colors of wires on some FTDI cable that hardly anyone uses anymore instead of the functions of the pins. Some of the clones don't do it this way.)

My worry is those numbers on the pot, the wiper is labeled as pin 3 where as normally the wiper is the central connection and labeled pin 2.

Grumpy_Mike:
My worry is those numbers on the pot, the wiper is labeled as pin 3 where as normally the wiper is the central connection and labeled pin 2.

Good call - I've seen new users wire pots up with one side of the supply on the wiper, so the values are varying based on how hard they load the supply and cause it's voltage to droop - though I'd reckon in this case the pot would be smoking... If he doesn't have GND connected wrong (I suspect he actually has this connected right - it sounds like he isn't working with a custom PCB based on that schematic, but rather drawing what he's prototyped) -

Hi,

Do you have a voltmeter / multimeter ? Borrow one? What is the voltage on A0 as you turn the pot?

Hi,
Have you soldered the pins to the promini PCB?


Pot connections;

Thanks.. Tom.. :slight_smile:
PS. I agree nice clean schematic, needs some layout refinements, but if it your first, good job..

Pot numbers seem off indeed, bottom of the pot is connected to GND but the line is awfully close to the edge of the image. What is the value of that pot? For large values (like 1M - which you probably want to save your battery) you will have to add a small cap between A0 and GND to stabilise the signal.

That button S1: you can wire it between GND and the pin, drop the resistor, and enable the internal pull-up.

ok guys, thank you all. It seems like I opened up a real discussion here. I'll answer you all.

You can also find the updated schematics:

  • ground was correctly connected to GND. My mistakes in the schematics
  • Vcc and GND do go to UNO. My missing in the schematics
  • I noticed a connection between GND and BLK in the soldering: could it be an issue? what is BLK?? it is not labeled on the mini pro

dave-in-nj
I'm not an expert about that, but it does the same also if I power the mini pro from an UNO (for uploading purposes). Do you thiink I should give it a try as well?

MorganS
thanks, I try to keep my projects clear for possible future modification: I won't remember where I live Tomorrow if I won't take a note...
Connections are coherent with the new schematics posted

allanhurst

  • pot is 2.4 kOhm, it is the pot from the arduino kit. No long leads: 10cm jumpers.
  • pot connection is correct: the image was cut really near to the line going to GND
  • I will give it a try on monday: I don't have any capacitor with me.
    Please explain: Why do you think it should help?

DrAzzy

  1. corrected as stated above
  2. corrected as stated above

Grumpy_Mike feat. DrAzzy :slight_smile:
Actually it is just an issue coming from EAGLE, the SW for schmatics drawing: I wasn't able to find my exact pot in the libraries, I just took one from the many. It is correct in the real soldering.

terryking228
from 3.3V to 0V. It seems fine

TomGeorge
Yes.
PS thanks, my second job. If you have any suggestion, please shoot

wvmarle

  • Pot numbering in the schematics are about what I said to Grumpy_Mike and DrAzzy
  • Pot value is 2.4 kOhm, why do you think I need larger values ? and why a capacitor should stabilize the signal? I'm not that expert...
  • btn S1 to which pin?

thank you everyone again
Giuseppe

piepolitb:

  • Pot value is 2.4 kOhm, why do you think I need larger values ? and why a capacitor should stabilize the signal? I'm not that expert...

Larger value for the pot means less loss of battery power. There's always a current running through it between Vcc and GND.

A small cap can stabilise the signal, and help the ADC for resistor values >>10k.

  • btn S1 to which pin?

Whichever you find convenient. They all have pull-up resistors.
Mind that this way the button becomes active low, the way you wired it the button is active high.

  • pot is 2.4 kOhm, it is the pot from the arduino kit. No long leads: 10cm jumpers.
  • pot connection is correct: the image was cut really near to the line going to GND
  • I will give it a try on monday: I don't have any capacitor with me.
    Please explain: Why do you think it should help?

The capacitor should smooth out any induced noise from stray electrical fields - eg nearby mains wiring.

But with such a low value pot and 10cm leads it shouldn't be a problem.

Perhaps that input is damaged - try a different one - A1,2,3 etc.

Allan

I think it is time to post a picture of your wiring so we can check it for things you might not know about.

Hi,
The BLK has me puzzled, I gather it is a component from your CAD.
Here is a promini pinout diagram and there is no BLK.

Tom... :slight_smile:

It is on this diagram:-

Top left corner, looks like a colour coded ribbon cable.

Hi,
Make R4 = 10K, 200R is too low for a pull down resistor.

Interesting the two pinout diagrams are mirror images of FTDI connection.

Thanks.. Tom.. :slight_smile:

allanhurst:
Perhaps that input is damaged - try a different one - A1,2,3 etc.

Allan

may or may not be able to use A1, the sketch calls it to read battery voltage, but it is absent on the schematic.

#define potPin A0
#define batPin A1

Hi,
What is the 3.7V battery, how big?

Can you connect DMM across it and monitor the voltage as the analog reading changes?

Can you post a picture of your project please, so we can see your component layout?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Make R4 = 10K, 200R is too low for a pull down resistor.

Interesting the two pinout diagrams are mirror images of FTDI connection.

Thanks.. Tom.. :slight_smile:

Well, you know how to debug most serial connection problems: Swap TX and RX. If that doesn't work, swap them again. Two swaps fix most problems. Unless you need to make a left turn. Then three rights make a left.