2

I have no idea why this is happening, but after I open COM port to talk with my Arduino (I click "Open" in RealTerm), the state of some pins goes from high to low for about 2 seconds and then goes back to high. Here's my code:

void setup()
{
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  Serial.begin(9600);
}

void loop()
{
}

In this case pin 5 will behave this way. I've checked it also for pin 6 and pin 8 and they behave in the same manner. I suppose more pins act like that. Why is this happening?

2
  • 3
    That's because opening the connection to the UNO will automatically trigger a reset of the ATmega chip, hence your sketch will be executed from scratch, ie setup() will be called again then loop().
    – jfpoilpret
    Jan 6 '15 at 17:20
  • 1
    @jfpoilpret you should put that as an answer!
    – sachleen
    Jan 6 '15 at 19:12
3

What you see is expected: that's because opening the USB connection to the UNO will automatically trigger a reset of the ATmega chip of the board, hence your sketch will be executed from scratch, ie setup() will be called again then your loop() repeatedly.

Hence what you observe in detail:

  1. open USB connection from PC
  2. open USB connection on Arduino UNO
  3. UNO USB chip (an ATmega16u2) triggers a RESET of the MCU (an ATmega328P-PU)
  4. On reset, the MCU sets all pins to INPUT, i.e. high impedence, hence no voltage can be measured on any pin
  5. Then the bootloader starts, performs some intialization and starts your sketch
  6. Your sketch executes first setup(), so your pin is put in OUTPUT mode and set to HIGH: its voltage is thus measurable (5V)
  7. your sketch loop() gets executed "forever" i.e.until a new reset or power supply disconnection
3
  • Indeed. Worth noting though is that this reset-on-open is ultimately due to a configurable behavior of how the PC operating system's serial API manages the control lines - though this is usual default. Additionally, it can be disabled at the arduino end by overpowering the reset capacitor with a stronger pullup. Jan 6 '15 at 22:48
  • What is the logic behind resetting upon connection? What exactly happens when I click "Open"? Jan 7 '15 at 9:06
  • That is because the bootloader must be started in order to check if connection was open in order to upload a new sketch. This is because USB can be used for both situations: upload a sketch, or just communicate with the running sketch.
    – jfpoilpret
    Jan 7 '15 at 9:13
0

The arduino is reseting because of the serial port initializing. If you want to avoid it, you can put a 120 ohm resistor between 5v and reset.

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.