element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Analog pin value keep fluctuating in Arduino
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 10 replies
  • Subscribers 393 subscribers
  • Views 2592 views
  • Users 0 members are here
  • switch
  • arduino
Related

Analog pin value keep fluctuating in Arduino

Former Member
Former Member over 10 years ago

I  tried reading analog input for a switch.But the analog value is moving gradually from 0 to 1023 and back to 0 ,eventhough the switch is not pressed.

When switch is pressed,it reaches 1023.This is agreeable.

But when switch is not pressed,it reaches 1023.Please help me to resolve the issue.This will solve my other issue automatically...

I am using 20K ohm resistor.


#include <SPI.h>

#include <PubSubClient.h>

#include <Ethernet.h>

#include <util.h>

#include <ctype.h>

 

 

#define CLIENTID "ArduinoSensor"

#define TOPICNAME "switch/signal"

#define POLLINTERVAL 120000

 

#define PORT 80

//Connect to MQTT server

byte server1 [] = { X};

byte mac[] = {X};

 

IPAddress arduinoIP(X);

IPAddress dnsIP(X);

IPAddress gatewayIP(X);

IPAddress subnetIP(X);

EthernetServer server(PORT);

 

EthernetClient ethClient;

PubSubClient arduinoClient(server1, 8081, callback, ethClient);

 

 

void callback(char* topic, byte* payload, unsigned int length) {

  // handle message arrived

}

 

 

int analogPin = 0;     // potentiometer wiper (middle terminal) connected to analog pin 3

 

                       // outside leads to ground and +5V

 

int val = 0;           // variable to store the value read

 

 

 

void setup()

 

{

 

  Serial.begin(9600);          //  setup serial

 

Ethernet.begin(mac, arduinoIP, dnsIP, gatewayIP, subnetIP);

beginConnection() ;

}

 

void beginConnection() {

Serial.begin(9600);

//Ethernet.begin(mac) ;

int connRC = arduinoClient.connect(CLIENTID) ;

if (!connRC) {

Serial.println(connRC) ;

Serial.println("Could not connect to MQTT Server");

Serial.println("Please reset the arduino to try again");

delay(100);

exit(-1);

}

else {

Serial.println("Connected to MQTT Server...");

 

 

}}

 

void loop()

 

{

 

val = analogRead(analogPin);    // read the input pin

  Serial.println(val);
delay(100);  // debug value
if(val == 1023)
{
Serial.println("Button pressed and message published");
arduinoClient.publish(TOPICNAME, "Button A is Pressed") ;

}

}

 

Its working better...But I see a problem with the analogRead...

The readings goes as below,

0

0

0

0

19

20

49

71

117

126

227

322

451

571

702

809

923

1023

Button pressed and message published

1023

Button pressed and message published

1023

Button pressed and message published

1022

1003

966

929

905

842

748

615

480

356

229

122

1

0

0

0

0

0

0

 

The readings gradually increasing till 1023 and then decreasing gradually.This should not happen as it is publishing messages when it is reaching 1023 without even pressing the switch.

But When the switch is pressed,it reaches 1023 and publishing the message.This is fine as this is required scenario..

 

Can you please help how to control the flow of analogRead?If this is kept under control,then my problem would be resolved...

  • Sign in to reply
  • Cancel
Parents
  • balearicdynamics
    balearicdynamics over 10 years ago

    Hello,

     

    I think that the reason of the fluctuation is just the switch. When you press the switch, the analog input receives a value, that is the highest and you see that it decreases if you put a resistor in series. Instead when the switch is not pressed, the analog input does not receives signals and - exactly as we can expect, the internal DA converter fluctuates. You get the same result if you try to read another analog pin with nothing connected.

     

    If you need read a switch on / off from the analog pin, I suggest to send the Vref to both the switch sides but to one of the two (e.g. the off position) you can put a resistor, e.g. 5K or more. So, then the switch is off, you receive a fixed value while then the switch is pressed the resistor is bypassed and you receive the highest value (1023, that you can assume it is the digital "1")

     

    Just a note: as you write you are already using a 20K resistor it is better if you put the scheme so it is possible to have an idea of what should be expected.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 10 years ago in reply to balearicdynamics

    Hi Enrico,

    Thanks for your reply..

    I am not from Electronics background...I could not understand the setup you have mentioned for noise reduction..

    Could you please show in circuit diagram or in anyother format so that I can buy necessary components and try the configuration..?

     

    Thanks,

    Radha

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to Former Member

    Hello,

     

    what I have asked is the circuit that you have done. Please put the drawing of the circuit so I figure out what you are doing. Else it is almost impossible.

     

    Thank you. Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 10 years ago in reply to Former Member

    generk4

         Hello, there is a problem of reading a switch which should have a value of high or low with analog which is like a variable resistor. If the values are changing, you do not have the leg of the switch pulled to ground. In other words the switch is floating. You claimed it is 20K. Here is code and a picture of connecting a switch from arduino.cc:

     

    Code

    /* switch

    *

    * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button

    * press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's

    * a minimum delay between toggles to debounce the circuit (i.e. to ignore

    * noise). 

    *

    * David A. Mellis

    * 21 November 2006

    */

    int inPin = 2;      // the number of the input pin

    int outPin = 13; // the number of the output pi

     

    int state = HIGH; // the current state of the output pin

    int reading; // the current reading from the input pin

    int previous = LOW; // the previous reading from the input pin

     

    // the follow variables are long's because the time, measured in milliseconds

    // will quickly become a bigger number than can be stored in an int.

    long time = 0;                   // the last time the output pin was toggled

    long debounce = 200;      // the debounce time, increase if the output flickers

     

    void setup()

    {

      pinMode(inPin, INPUT);

      pinMode(outPin, OUTPUT);

    }

     

    void loop()

    {

         reading = digitalRead(inPin);

     

      // if the input just went from LOW and HIGH and we've waited long enough

      // to ignore any noise on the circuit, toggle the output pin and remember

      // the time

      if (reading == HIGH && previous == LOW && millis() - time > debounce) {

        if (state == HIGH)

          state = LOW;

        else

          state = HIGH;

     

        time = millis(); 

      }

      digitalWrite(outPin, state);

     

      previous = reading;

    }

     

    PushButton.jpg

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 10 years ago in reply to balearicdynamics

    Hi Enrico,

    This is what I have done for the analog input,

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 10 years ago in reply to clem57

    Thanks Clem.You have given me digital input code.I will try and update the status..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • dmaruska
    dmaruska over 10 years ago in reply to Former Member

    This looks wired incorrect.  The Analog line while the button is open, is floating unless you are using a internal pull up resistor.  The schematic should be like this.    Your Analog line is HELD high until you push the button. I did not change the resistor value, but 10K will work just fine.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • dmaruska
    dmaruska over 10 years ago in reply to Former Member

    This looks wired incorrect.  The Analog line while the button is open, is floating unless you are using a internal pull up resistor.  The schematic should be like this.    Your Analog line is HELD high until you push the button. I did not change the resistor value, but 10K will work just fine.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube