element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Blog Why does it work...sometimes?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: colporteur
  • Date Created: 9 Jul 2022 2:58 PM Date Created
  • Views 1741 views
  • Likes 6 likes
  • Comments 8 comments
  • arduino-troublshooting
  • arduino_development
Related
Recommended

Why does it work...sometimes?

colporteur
colporteur
9 Jul 2022

Why does this code loaded on a Nano knockoff work sometimes, if I clearly missed a section of code?

int LEDPin = 9;
int ButtonPin = 8;
int RelayPin = 2;

void setup() {

// initialize digital pin LED_BUILTIN as an output.
pinMode(LEDPin, OUTPUT);
pinMode(ButtonPin, INPUT); //Set the switch's pin to be an input with a pullup (default is HIGH)

pinMode(RelayPin, OUTPUT);

digitalWrite(RelayPin, HIGH);

}

I wrote some Arduino code to control a relay when a button is pressed. It worked on the bench at home but in the lab it didn't work. Two different breadboard circuits same Nano. The issue was I failed to pinMode the OUTPUT to the relay shown in red.

My question to the aficionados of Arduino, those individuals with much more knowledge and experience with Arduinos than moi, is why did it work? Uploading and compiling the code produced no warnings. At least none that I have become accustome to making. On one breadboard layout, the circuit worked but on another breadboard layout, it did not. I sheepishly confess it took me a few cycles to figure out what I had done wrong. This required multiple trips between the lab and home so I know it wasn't just a freak one-time event.

Any insight you can share loads up my toolkit for the next time.

  • Sign in to reply
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to colporteur
    • "Why would the undetermined on one setup be HIGH and on another it would be LOW?"

    That is compliant with the definition of Undetermined. You can't rely on it. Maybe the different stray capacity of the two setups makes it behave in a different unreliable way. But it's definitely unreliable, as per the documentation.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • colporteur
    colporteur over 3 years ago in reply to misaz

    I just realized an additional code change I made.

    There was no external resistor. I employed the INPUT_PULLUP on the button pin in the code I had the issue. The code version shown is an update going from a ground trigger to a +5V trigger to resolve another use case.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rsjawale24
    rsjawale24 over 3 years ago in reply to rsjawale24

    Also, as the name suggests undetermined state. Which means it can be LOW or HIGH. It is unpredictable

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rsjawale24
    rsjawale24 over 3 years ago in reply to colporteur

    The answer lies within the architecture of the MCU. If you take a look at the circuit diagram of the port of any MCU, you'll see that there is typically a buffer and/or a latch that drives the physical pin of the MCU. The voltage in high Z case depends upon what the value of the latch/buffer is on power-up. Some pins on MCUs don't have internal pull up they are open-drain/collector, hence a pull-up resistor is needed for driving loads/passives. This is at least valid for 8051 as I have studied that architecture in detail. I'm pretty sure that AVR has a similar working structure for its ports/pins.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • misaz
    misaz over 3 years ago

    Do you connect pull-up resistor on button pin? Describe or post schematics of your button connection.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
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