Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: Strange readings on A0..

  1. #1

    Strange readings on A0..

    Hey folks, I built a 4 button circuit as per https://github.com/dxinteractive/AnalogMultiButton, essentially to read the four pushbuttons through a single analog pin. Now, I built this on the Mega, and got steady 1023 readings while no button was pressed - I believe this is the expected behavior - the number would change depending on which button I was pressing. Fine.

    Now I've attached it to the Teensy 3.6 (image attached)
    Click image for larger version. 

Name:	IMG_2181.jpg 
Views:	15 
Size:	173.2 KB 
ID:	12596

    and I'm getting very noisy readings from A0 with no buttons pressed. I've even tried running a 3.3v line directly to the A0 pin to see if I'd get steady 1023 then.. Nope.

    Here's what I'm seeing:

    Code:
    803
    804
    820
    806
    818
    812
    825
    814
    795
    794
    804
    819
    800
    805
    819
    808
    821
    816
    815
    803
    817
    793
    822
    806
    809
    807
    782
    804
    805

    and here's the code - essentially the example code, with a button added.

    Code:
    // include the AnalogMultiButton library#include <AnalogMultiButton.h>
    
    
    // define the pin you want to use
    const int BUTTONS_PIN = A0;
    
    
    // set how many buttons you have connected
    const int BUTTONS_TOTAL = 4;
    
    
    // find out what the value of analogRead is when you press each of your buttons and put them in this array
    // you can find this out by putting Serial.println(analogRead(BUTTONS_PIN)); in your loop() and opening the serial monitor to see the values
    // make sure they are in order of smallest to largest
    const int BUTTONS_VALUES[BUTTONS_TOTAL] = {1, 962, 510,612};
    
    
    // you can also define constants for each of your buttons, which makes your code easier to read
    // define these in the same order as the numbers in your BUTTONS_VALUES array, so whichever button has the smallest analogRead() number should come first
    const int BUTTON_BLUE = 0;
    const int BUTTON_YELLOW = 1;
    const int BUTTON_GREEN = 2;
    const int BUTTON_RED = 3;
    
    
    // make an AnalogMultiButton object, pass in the pin, total and values array
    AnalogMultiButton buttons(BUTTONS_PIN, BUTTONS_TOTAL, BUTTONS_VALUES);
    
    
    // pass a fourth parameter to set the debounce time in milliseconds
    // this defaults to 20 and can be increased if you're working with particularly bouncy buttons
    
    
    void setup() {
      // begin serial so we can see which buttons are being pressed through the serial monitor
      Serial.begin(115200);
    }
    
    
    void loop() {
      
      
      // update the AnalogMultiButton object every loop
      buttons.update();
      Serial.println(analogRead(BUTTONS_PIN));
      // check if BUTTON_RED is pressed
      if(buttons.isPressed(BUTTON_RED))
      {
        // Serial.println("Button red is pressed");
      } else {
        // Serial.println("Button red is not pressed");
      }
    
    
      // check if BUTTON_GREEN has just been pressed this update
      if(buttons.onPress(BUTTON_GREEN))
      {
        Serial.println("Green has been pressed");
      }
    
    
    // check if BUTTON_YELLOW has just been pressed this update
      if(buttons.onPress(BUTTON_YELLOW))
      {
        Serial.println("Yellow has been pressed");
      }
      // check if BUTTON_GREEN has just been released this update
      if(buttons.onRelease(BUTTON_GREEN))
      {
        Serial.println("Green has been released");
      }
    
    
      // do this if BUTTON_BLUE has been released
      if(buttons.onRelease(BUTTON_BLUE))
      {
        Serial.println("Blue has been released");
      }
    
    
      // do this once if BUTTON_BLUE has been held for 1 second
      if(buttons.onPressAfter(BUTTON_BLUE, 1000))
      {
        Serial.println("Blue has been down for 1 second");
      }
    
    
      // do this contantly if BUTTON_GREEN has been held down for less than a second
      if(buttons.isPressedBefore(BUTTON_GREEN, 1000))
      {
        Serial.print("Green is held for ");
        Serial.print(buttons.getPressDuration());
        Serial.println(" ms");
      }
    
    
      // do this contantly if BUTTON_RED has been held down for more than a second
      if(buttons.isPressedAfter(BUTTON_RED, 1000))
      {
        Serial.print("Red is held for ");
        Serial.print(buttons.getPressDuration());
        Serial.println(" ms");
      }
    
    
      // do this if BUTTON_BLUE was released, and it was held for 1 second or less
      if(buttons.onReleaseBefore(BUTTON_BLUE, 1000))
      {
        Serial.println("Blue has been released after less than 1 second of pressing");
        Serial.print("Blue was held for ");
        Serial.print(buttons.getLastReleasePressDuration());
        Serial.println(" ms");
      }
    
    
      // do this if BUTTON_BLUE was released, and it was held for 2 seconds or more
      if(buttons.onReleaseAfter(BUTTON_BLUE, 2000))
      {
        Serial.println("Blue has been released after at least 2 seconds of pressing");
        Serial.print("Blue was held for ");
        Serial.print(buttons.getLastReleasePressDuration());
        Serial.println(" ms");
      }
    
    
      //
      // More examples:
      //
      // do this once when BUTTON_BLUE is pressed, and again after 1 second
      // if(buttons.onPressAndAfter(BUTTON_BLUE, 1000)) {}
      //
      // do this once if BUTTON_BLUE is held for 1 second, and again every 0.5 seconds after that
      // if(buttons.onPressAfter(BUTTON_BLUE, 1000, 500)) {}
      //
      // do this once when BUTTON_BLUE is pressed, and again after 1 second, and again every 0.5 seconds after that
      // useful for cursors or scrolling through menu items
      // if(buttons.onPressAndAfter(BUTTON_BLUE, 1000, 500)) {}
      //
    
    
      delay(10);
    }
    Why am I getting such different behavior between the two platforms? and more importantly.. how can i fix this so I can get steady and reliable feedback from the analog pin? My understanding is that the analog pin should be giving me 0 - 1023 depending the voltage at the pin - 0v to 3.3v. Is this correct?

    Thanks...

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,033
    I'm running your program here on a Teensy 3.6 with a 1K resistor connected from 3.3V to the A0/14 pin.

    Click image for larger version. 

Name:	DSC_0957_web.jpg 
Views:	13 
Size:	132.8 KB 
ID:	12599

    I get a solid 1023 printing in the serial monitor.

    Click image for larger version. 

Name:	sc.png 
Views:	14 
Size:	44.7 KB 
ID:	12600

    Maybe try only a single resistor like this, just to confirm your Teensy is working.

    If so, try using a voltmeter when you reconnect the buttons, so you can confirm the actual voltage at the pin. My guess is something about the hardware might not be connected quite right?

  3. #3
    Thanks Paul,

    I tried your suggestion..

    Click image for larger version. 

Name:	IMG_0085.jpg 
Views:	13 
Size:	133.8 KB 
ID:	12601

    just a single resistor from the 3.3v rail to the A0 pin.. Here's my output..

    Click image for larger version. 

Name:	single resistor.jpeg 
Views:	16 
Size:	68.5 KB 
ID:	12602

    Please dont tell me somethings wrong with my Teensy.. I've only had it for two weeks!

    BTW... I should add that I've cut the VCCin pin, underneath the board, so the Teensy is no longer powered by the USB.. instead taking power from voltage regulator at 5v.

    Thanks
    Last edited by vfxwolf; 01-13-2018 at 08:08 PM.

  4. #4
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,459
    It might also be that something is wrong with your 3.3V rail or its wiring or the breadboard contacts. Before thinking of a defective Teensy, you should check with an oscilloscope if the 3.3V rail is stable and clean and then follow the signal up to the Teensy’s input pin to see where the signal gets dirty.

    Second, it is highly unprofessional for a circuit designer to order only one single Teensy. Shit and “oops” can happen at every moment, especially when breadboarding circuits with external peripherals. Thus, it is recommended to order your Teensies in batches of at least 3, so that you have always a few spares in the drawer and can quickly do differential diagnose.

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,432
    My guess is check your connections. That is make sure you have a good 3.3v being fed into your power pins...

    Also did you solder your pins or did they come that way? Check for any cold solder joint... Or try to touch all of the pins again with soldering iron.
    As Paul mentioned try to do it with Just the single resistor. Your picture still shows other wires connected.

  6. #6
    As you know, Theremingenieur, I'm not a professional circuit designer - I'm a filmmaker, remember?

    That said, I think it might be my breadboard contacts... when I tap the resistor, the values change. Is there a "best of breed" for breadboards? a trusted brand? Its not the first time I've noticed dodgy contacts with these Elegoo boards.

  7. #7
    Kurt,

    The Teensy came from Digikey with headers already attached... I wasnt about to mess with that. I'll try moving the teensy to a clean board.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,033
    Quote Originally Posted by vfxwolf View Post
    Please dont tell me somethings wrong with my Teensy.. I've only had it for two weeks!
    Something's wrong with your connections. I'd guess Teensy is probably working.

    In your photo, Teensy looks like it's plugged into some sort of socket, which then plugs into the breadboard. What is that part? Maybe show us a photo of the actual pins going into the breadboard?


    Quote Originally Posted by vfxwolf View Post
    Is there a "best of breed" for breadboards? a trusted brand?
    The one in my photo is this breadboard we sell.

    https://www.pjrc.com/store/breadboard.html

    We get these from a Canadian company. They probably get them from somewhere in China, but I can tell you I tried buying samples of several dozen Chinese companies. All were inferior to these ones from Canada. We really wanted to offer a breadboard in the $3 to $4 range like so many other companies in the maker market, but these really are better.

    Having said that, I'd also guess your breadboard probably isn't the issue. Those sockets and the wires are likely the problem. Wire mistakenly plugged in the wrong spot is the most likely scenario, so I'd also suggest removing all other wires so you're testing the simplest possible case (like I did in the photo I posted....)

    I'm always deeply suspicious of those "breadboard wires". They're made of the thinnest possible wire and break easily. I almost never use them. I use #22 solid wire, which you can see in the photos I often post here. It's also the same wire we put in this tutorial kit (but not shown in the photo). I believe it's Carol C2004 type.

  9. #9
    Pleased to report that on a fresh breadboard, I'm now getting solid 1023 values from A0. Imagine my relief!

    Paul, as you can see, I'm trying your solid core wire technique.. something terrifying about seeing wires without insulation or color-coding though, and obviously not as flexible, certainly reduces clutter, however.

    Click image for larger version. 

Name:	IMG_6035.jpg 
Views:	14 
Size:	145.8 KB 
ID:	12605

    A (I'm sure basic) electronics question.. when I'm running ground to the ground rail from the teensy, are GND and AGND interchangeable? or should I always take ground from the power, as I've in the picture.. (no doubt Theremingenieur will pop in here to tell me what a professional does)

    TIA

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,033
    They're not quite the same. When in doubt, just use GND.

    AGND can be used for analog signal grounds. In some cases that reduces noise. But unless you have something like LEDs or motors drawing substantial pulsed current through the GND wires, it's unlikely to make much difference.

  11. #11
    Actually, I'm building a motion-control camera rig, and ultimately, I'll have at least three motors being controlled by the Teensy... sooo.. AGND then?

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,033
    Sure, use AGND, but only for the grounds of analog signals. Connect all other stuff to regular GND.

    Again, it only makes a difference in certain scenarios.

    Your motors will probably need their own power source, right? Best to connect power as directly to the motors as possible, and then run a GND wire from the motor or motor controller/transistor/circuitry to Teensy. That style of connection will make much more difference than GND vs AGND.

  13. #13
    Yes.. the motors are running off 12v. I'm splitting the power to the MV in pins on the TMC motor drivers on one side, and on the other, running through an LM2596 voltage regulator tuned down to 5v for the Teensy, and I have a 3.3v rail out of the Teensy. So essentially the project has 12v, 5v and 3.3v rails.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •