🛠 Bouton poussoir et moniteur série sur la Duemilanove (avec ses 0,5 s de silence)
Objectives
Read a push button and display its state on the serial monitor — the famous DigitalReadSerial — while discovering two documented specifics of the Duemilanove: the 20 to 50 kΩ internal pull-ups activatable by software, and the auto-reset via the FTDI DTR line: each time the port is opened, the board resets and the ATmegaBOOT bootloader swallows the first bytes for ~0.5 seconds. Your automation scripts must keep this in mind.
Hardware
- 1 Arduino Duemilanove (ATmega328 recommended)
- 1 push button (4 pins: pairs are connected two by two)
- 1 male-to-male wire
- Mini-USB cable; FTDI driver installed
Complete code
// Bouton sur broche 2 -> GND, pull-up interne active
const byte BOUTON = 2; // les interruptions externes sont aussi sur 2 et 3
void setup {
pinMode(BOUTON, INPUT_PULLUP); // pull-up interne 20-50 kOhm
Serial.begin(9600); // USB via FTDI FT232RL
delay(1000); // laisser passer le bootloader (~0,5 s)
Serial.println("Duemilanove prete");
}
void loop {
// rappel : avec pull-up, bouton enfonce = LOW
byte etat = digitalRead(BOUTON);
Serial.println(etat == LOW ? "ETAT=0 (appuye)" : "ETAT=1 (libere)");
delay(100);
}
Expected verification
The serial monitor (9600 baud) continuously displays "ETAT=1 (libere)" [STATE=1 (released)]; holding the button down changes the lines to "ETAT=0 (appuye)" [STATE=0 (pressed)]. Each time the monitor is opened/closed, the board blinks (DTR reset) and the line "Duemilanove prete" [Duemilanove ready] reappears: this is the normal behavior of the auto-reset, not a bug.
Common errors
- Waiting for data in the half-second following the port opening: the ATmegaBOOT bootloader keeps the port for itself and the first bytes sent by the host are lost — wait ~1 s before talking to the board (documented on the official page).
- Button wired to 5 V without an external resistor while the pull-up is not activated in the code: the input floats and the state changes on its own.
- Inverting the logic: with INPUT_PULLUP, button pressed = LOW; many beginners test
== HIGHand think the button is dead. - Button on pins 0 or 1: they are connected to the FTDI (USB RX/TX) and hijack the serial monitor — prefer 2 and 3 (which are also the two external interrupts).
- Wrong baud rate on the monitor: the code opens at 9600, set the monitor to 9600.
- On older PCs, the COM port changes with each different USB port — check in the Device Manager.
- Exceeding 40 mA per pin (official spec): here the button consumes nothing, but do not plug an LED without a resistor out of habit.
Other tutorials for this board
- Faire clignoter la LED 13 d'une Duemilanove (et choisir le bon ATmega)
- Potentiomètre et variation LED : l'ADC 10 bits et le PWM 8 bits version AVR
- Ajouter un deuxième port série avec SoftwareSerial (GPS sur broches 10/11)
- Régler la référence de mesure avec AREF et analogReference
- Persister une calibration dans l'EEPROM de l'ATmega (512 octets ou 1 Ko)
- Convertir la Duemilanove en programmateur AVR (ArduinoISP) pour graver des puces nues
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinopwm-respiration.inoArduinopotentiometre-plotter.inoArduinoservo-balayage.inoArduinobuzzer-melodie.inoArduino