Sub topic 3
Keeping Credentials in a Separate File
This chapter assumes you can write and upload a basic sketch, use Serial output, and install libraries in the Arduino IDE or Arduino CLI. You should also know what an SSID (the name of a Wi-Fi network) is. By the end you will know how to move your Wi-Fi name and password out of the main sketch, keep that file out of version control, and recognize the limits of this approach.
Why credentials do not belong in the main sketch
A Wi-Fi sketch needs at least two private values: the network name and the password. Later chapters add more, such as MQTT broker passwords and API keys. The simplest approach is to write them as string literals next to the code that uses them:
WiFi.begin("HomeNetwork", "correct-horse-battery");This works, but it causes three problems.
- Sharing leaks secrets. You will post sketches in forums, send them to colleagues, and commit them to a public repository. Each time, the password goes with them unless you remember to remove it.
- Version history keeps secrets. If you commit a password and delete it in the next commit, it is still in the repository history. Anyone who clones the repository can read it.
- Changing networks means editing code. If the same sketch runs on boards at home and at a workshop, you have to edit and re-upload the logic just to change a string.
The fix is to put the values in a second file and include it from the sketch. The main sketch then contains no secrets and can be shared freely.
How the secrets header works
A header file is a text file that the compiler pastes into your sketch at the point of an #include line. Putting #define lines in a header gives you named constants that the sketch can use. A #define is a preprocessor instruction that replaces a name with a piece of text before compilation.
Here is the header file, named arduino_secrets.h:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"The name arduino_secrets.h is a convention, not a requirement. The Arduino IDE's cloud editor generates a tab with this name, and many Arduino examples use it, so following it makes your sketches familiar to other people. The SECRET_ prefix makes it easy to see in the main sketch which values are private.
The main sketch includes the file with double quotes rather than angle brackets:
#include <WiFiNINA.h>
#include "arduino_secrets.h"
void setup() {
Serial.begin(115200);
WiFi.begin(SECRET_SSID, SECRET_PASS);
}
void loop() {}The difference between the two forms matters:
| Form | Where the compiler looks first | Typical use |
|---|---|---|
| `#include <WiFiNINA.h>` | The installed library and board paths | Libraries |
| `#include "arduino_secrets.h"` | The sketch folder, then the library paths | Your own files |
If you write #include <arduino_secrets.h> with angle brackets, the compiler will not search your sketch folder and compilation fails with a "No such file or directory" error.
Adding the file to your sketch
In the Arduino IDE 2.x, open the sketch, use the tab menu (the three dots next to the file tabs), and choose New Tab. Name it arduino_secrets.h. The IDE creates the file in the sketch folder next to the .ino file. You can also create the file by hand in any editor, as long as it sits in the same folder as the .ino file.
With the Arduino CLI, the sketch folder is just a directory. A layout looks like this:
wifi_demo/
wifi_demo.ino
arduino_secrets.h
.gitignoreArduino compiles every .ino, .h, and .cpp file in the sketch folder together, so no extra build setting is needed.
Keeping the file out of version control
Moving the secrets into their own file is only half the job. If you run git add . and commit, Git will happily include arduino_secrets.h. You must tell Git to ignore it.
Create a .gitignore file in the sketch folder, or in the repository root, containing:
arduino_secrets.hA .gitignore file lists patterns of files that Git will not track. If the file is already in the repository, ignoring it does nothing, because Git keeps tracking files it has seen. In that case you must remove it from tracking while keeping your local copy:
git rm --cached arduino_secrets.h
git commit -m "Stop tracking arduino_secrets.h"Share a template instead
Other people who clone your project need to know which values to supply. A common pattern is to commit a template file with placeholder values and ignore the real one:
wifi_demo/
wifi_demo.ino
arduino_secrets.h.example
arduino_secrets.h (ignored, never committed)
.gitignoreThe .example file holds the same two #define lines as the real header, with obvious dummy text. Your README then says: copy arduino_secrets.h.example to arduino_secrets.h and fill in your values. A new user gets a clear compile error if they skip this step, which is better than a sketch that silently connects to the wrong place.
A slightly more defensive header
Placeholder values can cause confusing behavior. If someone forgets to edit the file, WiFi.begin is called with a network that does not exist, and the sketch retries forever with no hint about the cause. You can make that failure obvious with a compile-time check in the main sketch:
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#ifndef SECRET_SSID
#error "SECRET_SSID is not defined. Copy arduino_secrets.h.example to arduino_secrets.h and edit it."
#endif
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
Serial.print("Will connect to: ");
Serial.println(SECRET_SSID);
}
void loop() {}#ifndef means "if not defined." If the name is missing, #error stops compilation and prints your message. This catches a bad header at build time instead of at run time.
Printing the SSID is fine, since the name of a network is not sensitive in the same way as the password. Never print SECRET_PASS to the Serial Monitor. Serial logs are often pasted into forum questions.
Where the compiled secrets end up
This technique protects your source code, not your firmware. The #define values are pasted into the sketch at compile time, so the strings are stored in the compiled program on the board's flash memory. Anyone with physical access to the board and the right tools can read the flash and extract them. The same applies to the compiled .bin or .hex files that the build produces.
For this reason, treat the build output as secret too. Do not attach compiled binaries to public releases if they were built with real credentials. For a hobby project on a home network this risk is usually acceptable. For a product, or a board in a public place, it is not, and you should use a dedicated network with limited access, which the security chapter returns to.
Comparing the options
There are several ways to supply credentials. Each trades convenience against safety.
| Approach | Secrets in shared source? | Change without rebuilding? | Complexity |
|---|---|---|---|
| Literals in the main sketch | Yes | No | Lowest |
| Separate `arduino_secrets.h`, git-ignored | No | No | Low |
| Credentials stored in flash or EEPROM at setup time | No | Yes | Medium |
| Provisioning over Bluetooth or a temporary access point | No | Yes | High |
The separate header is the right default for learning and for most personal projects. The last two options suit devices you give to other people, because the end user can enter their own network details without compiling anything. They require extra code and are beyond this chapter.
Common errors
These problems appear often when you first use a secrets file.
arduino_secrets.h: No such file or directory. The file is not in the sketch folder, the name is spelled differently, or you used angle brackets in the#include. Check the folder contents and the quote style.'SECRET_SSID' was not declared in this scope. The header exists but does not define that exact name. Compare the spelling and capitalization in both files, since the preprocessor is case sensitive.- The board never connects, though the code compiles. The SSID or password contains a typo, or the network is 5 GHz only. Most boards in this tutorial support 2.4 GHz only. Also check for stray spaces inside the quotes.
- Special characters break the string. A password containing a double quote or a backslash must be escaped in C++. Write
\"for a quote and\\for a backslash, for example#define SECRET_PASS "pa\"ss\\word". - The file was committed anyway. The
.gitignoreentry was added after the first commit. Usegit rm --cachedas shown above, then change the password. - Edits to the header do not appear to take effect. In rare cases the IDE caches a build. Use the verify button again, or clear the build cache in the CLI with
arduino-cli compile --clean.
Exercises
Exercise
Why does #include "arduino_secrets.h" use double quotes while #include <WiFiNINA.h> uses angle brackets?
Show answer
Double quotes make the compiler search the sketch folder first, which is where your own file lives. Angle brackets search the installed library and board paths, which is where WiFiNINA lives. Using angle brackets for your own header would fail with "No such file or directory".
Exercise
You added arduino_secrets.h to .gitignore, but git status still shows changes to the file. What is the likely cause, and what two steps fix it?
Show answer
The file was committed before the ignore rule was added, so Git still tracks it. Run git rm --cached arduino_secrets.h and commit the change to stop tracking it. Because the password remains in older commits, also change the password on your router.
Exercise
A colleague says that because the password is in a git-ignored header, it cannot be recovered from the board. Is this correct?
Show answer
No. The header protects the source code only. The strings are compiled into the firmware in flash memory, so someone with physical access to the board can read them out. The risk is reduced by using a separate, limited network for the device.