Setting Arduino pins high by default

To set certain Arduino  pins to HIGH by default when your program starts, you can configure the pins as outputs in the setup() function and set them to HIGH using the digitalWrite() function.

Here’s a step-by-step guide of how to do this

1. Set the Pin Mode

First we set the pinMode() function to configure the pins as outputs.

2. Set the Pin to HIGH

Now we use the digitalWrite() function to set the desired pins to HIGH.

Example Code

In this example where pins 2, 3, and 4 are set to HIGH by default:

void setup() {
  // Configure pins 2, 3, and 4 as outputs
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);

  // Set pins 2, 3, and 4 to HIGH
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}

void loop() {
  // Main program loop (can be left empty if nothing else needs to happen)
}

Notes:

  • The pinMode() function must be called in the setup() function before using digitalWrite().
  • The HIGH state will apply voltage (typically 5V or 3.3V, depending on your board) to the pin.
  • Make sure there are no hardware conflicts on the pins you are setting to HIGH for example connecting them to GND directly without a resistor.

If you need to set more pins, you can use a loop to simplify the code:

void setup() {
  // Array of pins to set HIGH
  int pins[] = {2, 3, 4, 5, 6};
  
  // Loop through each pin
  for (int i = 0; i < 5; i++) {
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], HIGH);
  }
}

void loop() {
  // Main program loop
}

This will make it easy to set multiple pins to HIGH without repetitive code.

Related posts

VEML6070 ultraviolet light sensor on an Arduino

Programming an Attiny44 using an Arduino

Programming an Attiny85 using an Arduino