In this section we will quickly look at the commonly used in-built function in the Arduino C sketches. These functions will help us in performing basic tasks while interfacing with various devices. Unless you are attempting advanced sketches, the following functions will be very helpful in building most of the Arduino sketches. Apart from the below in-built functions, device specific header files and functions are also used - we will understand this aspect in the chapter for Day 2 while understanding how to use a header library for a sensor.
C Sketch function | Purpose |
pinMode(PIN-NUMBER, I/O-MODE) | This function is used to specify whether a particular Arduino pin will be used in Input or Output mode. |
digitalRead(PIN-NUMBER) | This function is used to read digital input from a digital pin. The input value is either LOW or HIGH and can be stored in a Boolean type variable. |
digitalWrite(PIN-NUMBER, SIGNAL-LEVEL) | Use this function to send a digital LOW or HIGH signal on a particular pin. |
analogRead(PIN-NUMBER) | This function is used to read analog input from analog pins. The input value can be stored in an integer type variable. |
analogWrite(PIN-NUMBER, SIGNAL-VALUE) | Use this function to send an analog signal on a particular pin. The signal value can be specified as an integer between 0 and 1023. |
delay(MILLI-SECONDS) | This function is used for halting the program execution for the specified number of milliseconds. |
delayMicroseconds(MICRO-SECONDS) | This function can be used to halt the program execution for the specified number of microseconds. |
Serial.begin(BAUD-RATE) | This function sets the baud rate for communication between the Arduino board and Arduino IDE's Serial Monitor window (also with any serial port communication program) |
Serial.println("MESSAGE") | This method is used to output a string message to the Serial Monitor. It results in printing each message in a separate line. |
Serial.print("MESSAGE") | This method is used to output a string message to the Serial Monitor. All messages sent are printed one after the other on the same line. |
isnan(VALUE) | This in-built function is used to determine whether a specified value is a number or not. |
tone(PIN-NUMBER, FREQUENCY, MILLI-SECONDS) | Use this function to play a sound of a particular frequency on a buzzer connected to a particular pin. |
pulseIn(PIN-NUMBER, LOGIC-LEVEL) | Use this function to read and measure the duration of an input signal on a particular pin. Based on the logic level specified as a parameter to the function, it will wait for and read the signal until its logic level changes. For example, when reading a HIGH signal, it waits for a HIGH input signal and measures the duration until the signal level changes to LOW. |
millis() | This function returns the number of milliseconds since the Arduino board was powered on and the program started running. |
As we write more and more Arduino sketches you will start realizing that a large portion of the sketches deal with reading input and writing output to and from digital and analog pins. So it is important that we understand the basic techniques employed for the purpose of I/O. In the next two sections we will briefly explore these two options.