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 A sneaky problem
  • 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
  • State Verified Answer
  • Replies 6 replies
  • Answers 2 answers
  • Subscribers 392 subscribers
  • Views 626 views
  • Users 0 members are here
  • signal
  • status
  • digital
  • tactile_switch
  • tactile
  • arduino
  • relay
Related

A sneaky problem

balearicdynamics
balearicdynamics over 10 years ago

Hi to all.

 

I have a sneaky problem with Arduino initial pin settings. When the program starts, it should put a pin to 0 accordingly with the initial program flag status. This signal will change the on/off status (i.e. powered status) of a micro relay activated by a NPN 2222 transistor. The circuit part works perfectly and the initialisation code is the following:

 

int powerStatus;

 

void setup() { 

  pinMode(8, OUTPUT); // Relay ON/OFF

  pinMode(7, INPUT);  // Power Button

 

  powerStatus = 0; // Initial power status relay

    // Power OFF the relay

    digitalWrite(8, 0);


}

 

What I expect is that the relay is intially in the OFF state.

Then in the loop function every time that a pushbutton is pressed the state is changed from ON to OFF and vice versa. This works without problems but when the Arduino board is powered and the program starts, the relay is powered.

The loop test program is the following:

 

void loop() {

  // Check if the button is pressed  

  if( (digitalRead(7) == 0) {

     // Invert the power status of the relay

     if(powerStatus == 0)

       powerStatus = 1;

     else

       powerStatus = 0;

    // Power ON or OFF the relay

    digitalWrite(8, powerStatus);

    delay(500);

  }

}

 

Some ideas where I have done mistakes ?

 

Thanks in advance. Enrico

 

FOUND THE MISTAKE

The relais works in negative logic driver by the NPN 2222 transistor controlled by pin  8: means that as a matter of fact, when the pin has the logical value 1 there is no activity on the transistor, there is no GND to the relay and it is OFF. The following is the working code including a serial monitor to see how the operation works.

 

#include <Streaming.h>

 

int powerStatus;

 

#define RELAY_ON 0

#define RELAY_OFF 1

 

void setup() {

  pinMode(8, OUTPUT); // Relay ON/OFF

  pinMode(7, INPUT);  // Power Button

 

  // First, setup the relay 

  powerStatus = RELAY_OFF; // Initial power status realis

  digitalWrite(8, powerStatus);

 

  // Init serial and dump setup status

  Serial.begin(19200);

  Serial << "setup() - START" << endl;

  Serial << "powerStatus = " << powerStatus << " - written to pin 8 (relay)" << endl;

  Serial << "setup() - END" << endl << endl;

}

 

void loop() {

 

  if(digitalRead(7) == 0) {

     if(powerStatus == RELAY_ON)

       powerStatus = RELAY_OFF;

     else

       powerStatus = RELAY_ON;

      

    digitalWrite(8, powerStatus);

    Serial << "powerStatus = " << powerStatus << " wrtieen on pin 8 (relay)" << endl;

 

 

    delay(1000);

  } // button pressed

 

} // loop

 

 

Message was edited by: Enrico Miglino

  • Sign in to reply
  • Cancel

Top Replies

  • balearicdynamics
    balearicdynamics over 10 years ago in reply to lokkytron +1
    Thank you for the support. As the code had to be correct, there was a problem in the logic. I have updated the question with the right code and the serial debug information. Thank you, lokkytron
Parents
  • lokkytron
    0 lokkytron over 10 years ago

    Hello Enrico Miglino

    The only thing I can see in these lines is that you are checking in the loop the value for pin 7...

    If the first time you check it, the value is 0, the condition is true and the value is inverted..

    I don't know if you can check the initial value of 7 in the first loop, just at the beginning of the first loop

    I don't know if I have explained it well.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to lokkytron

    Thank you for the support. As the code had to be correct, there was a problem in the logic. I have updated the question with the right code and the serial debug information.

     

    Thank you, lokkytron

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to lokkytron

    Thank you for the support. As the code had to be correct, there was a problem in the logic. I have updated the question with the right code and the serial debug information.

     

    Thank you, lokkytron

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • lokkytron
    0 lokkytron over 10 years ago in reply to balearicdynamics

    No problem Enrico Miglino...  I'm glad helping

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