Skip to content
Home » Arduino » Arduino Tutorials » Learn Arduino Programming

Learn Arduino Programming

    Arduino programming is very easy to learn, in this article, you will learn how to program Arduino boards easily. Even you don’t have any programming skills, you can program Arduino easily. By the end of this article, you will able to program any Arduino boards.

    The programs written for Arduino are called sketches.

    Structure of Arduino Program:

    void setup()
    {
    statements;
    }
    
    void loop()
    {
    statements;
    }
    

    The setup() function is called once when your program starts. Use it to initialize pin modes, or begin serial. It must be included in a program even if there are no statements to run.

    void setup()
    {
    pinMode(pin, OUTPUT); // sets the 'pin' as output
    }

    The function pinMode(pin, mode) is used in void setup() to configure a specified pin to behave either as an INPUT or an OUTPUT.

    pinMode(pin, OUTPUT); // sets ‘pin’ to output
    
    pinMode(pin, INPUT); // sets ‘pin’ to input

    There are also convenient pull-up resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed in the following manner:

    pinMode(pin, INPUT); // set ‘pin’ to input
    
    digitalWrite(pin, HIGH); // turn on pull-up resistors

    Pullup resistors would normally be used for connecting inputs like switches. Notice in the above example it does not convert pin to an output, it is merely a method for activating the internal pull-ups.

    Pins configured as  OUTPUT can provide 40  mA  (milliamps) of current to other devices/circuits. This is enough current to brightly light up an LED  (don’t forget the series resistor), but not enough current to run most relays, solenoids, or motors.

    Short circuits on Arduino pins and excessive current can damage or destroy the output pin, or damage the entire Atmega chip. It is often a good idea to connect an OUTPUT pin to an external device in series with a 470Ω or 1KΩ resistor.

    The loop() function runs continuously.

    void loop()
    {
    digitalWrite(pin, HIGH); // turns 'pin' on
    delay(1000); // pauses for one second
    digitalWrite(pin, LOW); // turns 'pin' off
    delay(1000); // pauses for one second
    }
    

    Functions in Arduino Programming

    digitalWrite(pin, value)

    Outputs either logic level HIGH or LOW at (turns on or off) a specified digital pin. The pin can be specified as either a variable or constant (0-13).

    digitalWrite(pin, HIGH); // sets 'pin' to high
    
    digitalWrite(pin, LOW); // sets 'pin' to low

    digitalRead(pin)

    Reads the value from a specified digital pin with the result either HIGH or LOW. The pin can be specified as either a variable or constant (0-13).

    value = digitalRead(Pin); // sets 'value' equal to  the input pin

    The following example reads a pushbutton connected to a digital input and turns on an LED connected to a digital output when the button has been pressed:

    int led = 13; // connect LED to pin 13
    int pin = 7; // connect pushbutton to pin 7
    int value = 0; // variable to store the read value
    
    void setup()
    {
    pinMode(led, OUTPUT); // sets pin 13 as output
    pinMode(pin, INPUT); // sets pin 7 as input
    }
    
    void loop()
    {
    value = digitalRead(pin); // sets 'value' equal to  the input pin
    digitalWrite(led, value); // sets 'led' to the button's value
    }

    analogRead(pin)

    Reads the value from a specified analog pin with a 10-bit resolution. This function only works on the analog in pins (0-5). The resulting integer values range from 0 to 1023.

    value = analogRead(pin); // sets 'value' equal to 'pin'

    Note: Analog pins unlike digital ones, do not need to be first declared as INPUT or OUTPUT.

    analogWrite(pin, value)

    Writes a pseudo-analog value using hardware enabled pulse width modulation (PWM) to an output pin marked PWM. On Uno, this function works on pins 3, 5, 6, 9, 10, and 11. The value can be specified as a variable or constant with a value from 0-255.

    analogWrite(pin, value); // writes 'value' to analog 'pin'

    A value of  0 generates a steady  0 volts output at the specified pin; a value of  255 generates a steady 5 volts output at the specified pin. For values in between 0 and 255, the pin rapidly alternates between 0 and 5 volts – the higher the value, the more often the pin is HIGH (5 volts). For example, a value of 64 will be 0 volts three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one quarter of the time and 5 volts three-quarters of the time.

    Because this is a hardware function, the pin will generate a steady wave after a call to analogWrite in the background until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

    Note: Analog pins, unlike digital ones, do not need to be first declared as INPUT or OUTPUT.

    The following example reads an analog value from an analog input pin, converts the value by dividing by 4, and outputs a PWM signal on a PWM pin:

    int led = 10; // LED with 220 resistor on pin 10
    int pin = A0; // potentiometer on analog pin 0 int value; // value for reading
    
    void setup()
    { // no setup needed
    }
    void loop()
    {
    value = analogRead(pin); // sets 'value' equal to 'pin' value /= 4; // converts 0-1023 to 0-255
    analogWrite(led, value); // outputs PWM signal to led
    }

    delay(ms)

    Pauses a program for the amount of time as specified in milliseconds, where 1000 equals 1 second.

    delay(1000); // waits for one second

    millis()

    Returns the number of milliseconds since the Arduino board began running the current program as an unsigned long value.

    value = millis(); // sets ‘value’ equal to millis()

    Note: This number will overflow (reset back to zero), after approximately 9 hours.

    Serial.begin(rate)

    Opens serial port and sets the baud rate for serial data transmission. The typical baud rate for communicating with the computer is 9600 although other speeds are supported.

    void setup()
    {
    Serial.begin(9600); // opens serial port sets data rate to 9600 bps
    }
    

    Note: When using serial communication, digital pins 0 (RX) and 1 (TX) cannot be used at the same time.

    Serial.println(data)

    Prints data to the serial port, followed by an automatic carriage return and line feed. This command takes the same form as Serial.print(), but is easier for reading data on the Serial Monitor.

    Serial.print(value); // displays the value in serial monitor, the next value is displayed side of previous.
    Serial.println(value); // displays the value in serial monitor, the next value is displayed in new line.

    Leave a Reply