How to do Arduino Coding for beginners

In Arduino, we can do coding in C and C++ languages.

We will discuss how can code in C++ language and some basic predefined functions in Arduino

Code structure will be as follow:

// #include     /* Here we can include pre/user defined libraries */
#define LED 13  /* Creating Macro for led pin */
void setup()    /* setup function  is used for initializing and configuration */
{               /* This function execute only once when arduino powered up */
pinMode(LED,OUTPUT);     /* Initializing the led pin as output */
}
void loop()              /* loop function is the main function */
{                        /* This function executed as iteration */
digitalWrite(LED,HIGH);  /* making the led pin high, led will glow */
delay(1000);             /* sleep for 1000 msec for blinking so visible for our naked eye*/
digitalWrite(LED,LOW);   /* making the led pin low, led will off */
delay(1000);             /* sleep for 1000 msec or 1 second*/
}

The above program is an example of led blinking of 1 second.

Predefined Functions:

Some of the predefined functions are:
pinMode(pin,mode);
digitalRead(pin);
digitalWrite(pin,value);
analogRead(pin);
analogWrite(pin,value);
pulseIn(pin,value);
delay(ms);

pinMode(pin,mode):

  • Configures the specified pin to behave either as an input or an output.

Structure:
pinMode(pin , mode)
• pin: the number of the pin whose mode you wish to set.
• mode: INPUT or OUTPUT.

digitalRead(pin):

  • Reads the value from a specified digital pin, returns either HIGH or LOW

Structure:
digitalRead(pin)
• pin: the number of the digital pin you want to read ( 0 to 13 )

digitalWrite(pin,value):

  • Write a HIGH or a LOW value to a digital pin.

Structure:
digitalWrite(pin , value)
• pin: the pin number
• value: HIGH or LOW

analogRead(pin):

  • Reads the value from the specified analog pin

Structure:
analogRead(pin)
• pin the number of the analog input pin to read from A0 to A5

analogWrite(pin,value):

  • Writes an analog value (PWM wave) to a pin.

Structure:
analogWrite(pin , value)
• pin: the pin to write to. Allowed data types:
• value: the duty cycle: between 0 (always off) and 255 (always on)
• PWM pins are 3,5,6,9,10 and 11.

pulseIn(pin,value):

  • Reads a pulse (either HIGH or LOW on a pin, For example, if the value is HIGH pulseIn waits for the pin to go HIGH starts timing, then waits for the pin to go LOW

Structure
pulseIn(pin , value)
• pin: the number of the pin on which you want to read the pulse. ( int)
• value: type of pulse to read: either HIGH or LOW. (int)

delay(ms):

  • Pauses the program for the amount of time (in milliseconds) specified as a parameter. (There are 1000 milliseconds in a second.)

Structure
delay(ms)
ms: the number of milliseconds to pause. Allowed data types: unsigned long

I will explain these functions with examples in the next blog.

Note: reference from arduino.cc

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: