Fix “redefinition of 'void setup()'” in Arduino
Your sketch has two setup() (or loop()) functions — two pasted examples or a second .ino tab. Merge them into one setup() and one loop().
Community draft — report a mistake
What you see
WeatherStation.ino: In function 'void setup()':
WeatherStation.ino:9:6: error: redefinition of 'void setup()'
void setup() {
^~~~~
WeatherStation.ino:1:6: note: 'void setup()' previously defined here
void setup() {
^~~~~
exit status 1
Compilation error: redefinition of 'void setup()'WeatherStation.ino: In function 'void setup()':
WeatherStation.ino:9:6: error: redefinition of 'void setup()'
void setup() {
^~~~~
WeatherStation.ino:1:6: note: 'void setup()' previously defined here
void setup() {
^~~~~
exit status 1
Compilation error: redefinition of 'void setup()'
Every Arduino sketch has exactly one setup() and one loop(). When you combine two examples, you get two of each, and the compiler stops at the second definition.
// One setup() that does both jobs
void setup() {
Serial.begin(115200);
dht.begin(); // from the sensor example
lcd.init(); // from the display example
lcd.backlight();
}Quick fix — try in order
- 1
Merge the two setup() functions
Copy the lines from inside the second
setup() { … }into the first, then delete the second one completely. Do the same forloop(). - 2
Check other tabs
All .ino tabs in a sketch folder are joined into one file before compiling. A second tab with its own setup() — often a pasted example — causes this error. Delete or rename it to .txt.
- 3
Check the sketch folder
An old
Copy_of_sketch.inositting in the same folder is compiled too. Move it out. - 4
Merge globals and includes too
Each example also has its own
#includelines and global objects; keep one of each and rename clashes (e.g. twodisplayobjects).
Why it happens
Combining two examples
The classic case: a DHT example and an LCD example pasted into one file. Each brings its own setup() and loop(). A sketch has exactly one of each.
redefinition of a variable or object
redefinition of 'DHT dht' — the same global declared twice. Keep one.
multiple definition vs redefinition
Redefinition is caught by the compiler inside one file (all .ino tabs count as one). Duplicates across .cpp files show up later as a linker error — see multiple definition of.
Board-specific notes
Arduino IDE 2: Tabs are shown along the top of the editor. The folder name must match the main .ino name.
Did this fix it?
Thanks — that helps us keep this page right.