Lets see some basic functions related to Startup and GPIO that are frequently used in Arduino IDE.
/* Blinking on-board LED connected to pin 13 of Arduino UNO */
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
pinMode(13, OUTPUT); /* Pin 13 is defined as Output */
}
/* Loop runs over and over after the startup function */
void loop()
{
digitalWrite(13, HIGH); /* Make pin 13 High, LED ON */
delay(1000); /* Wait for 1 second */
digitalWrite(13, LOW); /* Make pin 13 Low, LED OFF*/
delay(1000); /* Wait for 1 second */
}
Reading analog value from an analog sensor connected to pin A1 of Arduino and displaying the ADC value on serial monitor of Arduino
/* Read analog value on analog pin A1 of Arduino UNO and print the ADC value on Serial Monitor*/
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
Serial.begin(9600); /* opens serial port, sets data rate to 9600 bps */
}
/* Loop runs over and over after the startup function */
void loop()
{
int adc_val;
adc_val = analogRead(A1); /* Read analog signal present on pin A1 */
Serial.print("ADC value is : "); /* Print */
Serial.println(adc_val); /* Print with \r\n */
delay(5000); /* Wait for 5 seconds */
}
Vary intensity of LED connected to pin 5 of Arduino by generating PWM wave of varying duty cycle
/* Vary intensity of external LED connected to pin 5 of Arduino UNO */
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
pinMode(5, OUTPUT); /* Pin 5 is defined as Output */
}
/* Loop runs over and over after the startup function */
void loop()
{
for(int i =0; i<256; i++)
{
analogWrite(5, i); /* Vary intensity of LED
connected externally to pin 5 of Arduino */
/* Vary the intensity by applying PWM of duty cycle varying
from 0 to 100% (writing value 0 to 255) */
delay(300); /* Wait for 300 milliseconds */
}
}