Code basics – Arduino pins

The most important feature of the Arduino is its control over digital input/output (I/O) pins. On each pin, we can set a voltage value of 5 V, representing logic HIGH, or 0 V, representing logic LOW. Also, we can read whether a value of 5 V or 0 V is applied externally. Here we will learn how.

Getting ready

For this recipe, ensure that you have the Arduino IDE running on a computer.

How to do it…

The following code turns a pin HIGH and LOW repeatedly while reading the external voltage applied to another:

void setup() {
  // Set pin 2 as a digital Output
  pinMode(2, OUTPUT);
  // Set pin 3 as a digital Input
  pinMode(3, INPUT);
}

void loop(){
    // Set pin 2 HIGH
  digitalWrite(2, HIGH);
  // Wait 100 milliseconds
  delay(100); // Set ...

Get Arduino Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.