Programming language is the way in which we convey our algorithm and mathematical models to our microcontroller. Coding in Arduino is done in a ‘C (programming language)’ like fashion.
The Structure of Arduino code comprises to two major parts.
- void Setup() contains the part of code which will only run once. The part of code written here is used to initialize the microcontroller, that is, here we mostly do only two things.
- Define the purpose of the pin: Analog and Digital pins can be used as either input or output, Therefore it is important to declare whether the pin is being used as an input or output. The Syntax for this declaration is:
pinMode(pin number(A0, A1…/2, 3, 4…), INPUT/OUTPUT);
For analog pins the ‘pin number’ is replaced by A0, A1 or A3 (one of it). For digital pins the ‘pin number’ is replaced by 2, 3 or 4 (one of it). And after the comma ‘,’ the mode of the pin is defined i.e. either INPUT or OUTPUT (one of it). It is important to notice that the above statement is case sensitive. And each statement ends with a semi-colon ‘;’.
2. Initialize Serial Communication: Serial communications is initialized in order to either work with a serial communications device like Bluetooth or Wi-Fi via shields or to display the inputs on our Laptop screen via Serial monitor in Arduino IDE. The syntax of initializing serial communication in Arduino is:
In simple terms baud rate is the rate of communication between communication device and Arduino. But we usually use 9600 as the baud rate i.e. Serial.begin(9600); The baud rate varies from device to device.
- void loop() contains the part of code which will run continuously in a loop. For example, if you write the code of switch an LED on and off, the LED will blink continuously until the power to Arduino is cut off. 1. Here we can take in digital or analog inputs as well as give out both analog and digital outputs:
|Digital||digitalRead(pin number)||digitalWrite(pin number, HIGH/LOW);|
|Analog||analogRead(pin number)||analogWrite(pin number, 0-255)|
For digital inputs we can replace the pin number from 2 to 13, depending upon pin on which the sensor is attached. Similarly for analog we can replace the pin number to be from A0 to A5.
In case of giving outputs, on digital pins ‘HIGH’ is command is given to turn the LED or buzzer or any other component on and ‘LOW’ command is given to turn off LED, buzzer or any other component along with the respective pin number. While for giving out analog outputs on PWM pins of Arduino (digital pins with ῀ symbol i.e. pin number: 3, 5, 6, 9, 10, and 11). Rather than giving out HIGH or LOW we give a number between 0-255 | 0 means off, 255 means on.
Trackbacks and pingbacks
No trackback or pingback available for this article.