Skip to main content

Arduino Code Basics - C, Pins

Code Basics -Arduino C

The Arduino uses a slightly reduced C/C++ programming language. In this recipe, we will remember a few basics of C/C++.
Ensure that you have Arduino IDE Installed on your Laptop/PC.

Here is a simple example of basic Arduino C/C++ manipulating two variables:

// Global Variables
 int var1 = 10;
 int var2 = 20;
void setup() {
  // Only execute once when the Arduino boots
  var2 = 5;  // var2 becomes 5 once the Arduino boots
}
void loop(){
  // Code executes top-down and repeats continuously
  if (var1 > var2)
{
 // If var1 is greater than var2
var2++; // Increment var2 by 1  
}
else
{
 // If var1 is NOT greater than var2
    var2 = 0; // var2 becomes 0  
}
 }

When we upload a sketch to the board, the Arduino software first compiles the code. If there is an error in the code, it will write it in the Status Display area and will stop the upload. If no errors are found, it will begin writing the compiled code to the board.
Errors will appear if the board or serial port is not properly selected. When everything is correctly set up, the TX RX LEDs will blink, meaning data is being transferred between the computer and the Arduino board. When the transfer is done, the board will reset and the code will immediately begin executing.
The code is stored in the Arduino board until it is erased or replaced by another code.  We can take the board and plug it into a battery or to another computer, and it will still  execute this blinking.

Code Basics - Arduino Pins
The most important feature of the Arduino is its control over digital input/output (I/O) pins. On each pin, we can set a voltage value of 5 V, representing logic HIGH, or 0 V, representing logic LOW. Also, we can read whether a value of 5 V or 0 V is applied externally. Here we will learn how.

The following code turns a pin HIGH and LOW repeatedly while reading the external voltage applied to another:

void setup() {
  // Set pin 2 as a digital Output
  pinMode(2, OUTPUT);
  // Set pin 3 as a digital Input
  pinMode(3, INPUT);
}
void loop(){
// Set pin 2 HIGH
digitalWrite(2, HIGH);
>
// Wait 100 milliseconds
delay(100);
// Set pin 2 LOW
digitalWrite(2, LOW);
// Wait 100 milliseconds
delay(100);
// Read the value of pin 3 and store it in a variable
int pinValue = digitalRead(3);
}

The code sets two pins in output and input mode and then writes and reads from them. Here is the code breakdown:

In setup(), we use the pinMode() function to set pin number 2 as an output. When we set a pin as an output, we can set that pin as either HIGH (5 V) or LOW (0 V). Also, we set pin number 3 as an input. A pin configured as input can read external voltages applied to it. It  can read HIGH if the voltage is around 5 V and LOW if the voltage is close or equal to 0 V:

void setup() {
  // Set pin 2 as a digital Output
pinMode(2,OUTPUT);
// Set pin 3 as a digital Input
pinMode(3,OUTPUT);
}

In the loop() function, we use the digitalWrite() function to set pin number 2 to  HIGH. Then, we wait for 100 milliseconds using the delay() function. This function stops  the execution of the code for the specified time, in milliseconds. Thereafter, we set the pin to LOW and wait another 100 milliseconds. In the end, we read the value of pin 3 in a variable:

void loop(){
// Set pin 2 HIGH
  digitalWrite(2, HIGH);
  // Wait 100 milliseconds
  delay(100);
  // Set pin 2 LOW
  digitalWrite(2, LOW);
  // Wait 100 milliseconds
  delay(100);
    // Read the value of pin 3 and store it in a variable
 int pinValue = digitalRead(3);
}

Comments

Popular posts from this blog

7400 - Quad Two Input NAND Gate

7400 - Quad Two Input NAND Gate 74LS00 is part of the 74XXYY IC series. 74xxyy integrated circuits are logical gates of digital electronics. The 74LS00 IC has four NAND gates(NAND Gate is the combination of AND and NOT Gate). In addition, each door has two entrances. Hence the name QUAD TWO INPUT NAND GATE.     Pin Diagram of 7400 IC 74LS00 is a 14-pin device. The chip is available in different packages and is chosen according to the needs.  The description of each pin is given below. Pin Number Description 1 A1-INPUT1 of GATE 1 2 B1-INPUT2 of GATE 1 3 Y1-OUTPUT of GATE1 4 A2-INPUT1 of GATE 2 5 B2-INPUT2 of GATE 2 6 Y2-OUTPUT of GATE2 9 A3-INPUT1 of GATE 3 10 B3-INPUT2 of GATE 3 8 Y3-OUTPUT of GATE3 12 A4-INPUT1 of GATE 4 13 B4-INPUT2 of GATE 4 11 Y4-OUTPUT of GATE4 7 GND- Connected to ground 14 VCC-Connected to positive voltage to provide smdksmkm power to all four gates Internal Ci

Arduino Mega 2560

Arduino Mega - Description & Pinout Surely Arduino UNO is one of the most popular Microcontroller development board for project hobbyist and for some professionals also. When cheaper boards are available, why choose Arduino Mega? The main reason behind this is the additional features that are built into this board. The first feature is the large I/O system with 16 analog and 54 built-in digital pins supporting USART and other communication modes. Secondly, it has a built-in RTC and other features like an analog comparator, an advanced timer, an interruption of the controller's wake-up mechanism to save more power and a faster speed with a 16MHz crystal clock to get 16 MIBS. It has more than 5 pins for Vcc and Gnd to connect other devices to Arduino Mega. Other features include JTAG support for programming, debugging, and troubleshooting. With a large FLASH and SRAM memory, this card can easily handle a large system program. It is also compatible with different typ