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 ...

Arduino Basics - Software, Code Basics

Arduino Basics - Software, Code Basics Arduino is an open source family of electronic microprocessor boards that we can easily program to understand and interact with the environment. Over the years, Arduino has become the standard for building electronics projects. Arduino has been sent into space to run micro satellites ,  it has been sent to the bottom of the ocean to control small robotic submersibles ,  and now, Arduino has arrived for you. To learn the basics of electronics refer this 4 Book -  Electronic circuits handbook for design and applications Electronics – the Basics Foundations of analog and digital electronic circuits The Art of Electronics Downloading the Arduino software . The first thing we need is the Arduino Integrated Development Environment (IDE). One of the best parts about Arduino is that the software in which we need to program the boards is free and open source. The Arduino IDE is compatible with Windows, Mac OS X,...

ESP8266 - WiFi Module

ESP8266 - Wi-Fi Module Content 1 Introduction 2 Features of ESP8266 3 How to Interface ESP8266 with Arduino Uno 3.1 Hardware and Software required 3.2 Circuit Diagram 6 Program for ESP8266 Introduction to ESP8266 The ESP8266 Wi - Fi Module is a self contained SOC with integrated TCP/IP protocol stack that can give any micro - controller access to your Wi - Fi network. The ESP8266 is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor.This module has a powerful enough on-board processing and storage capability that allows it to be integrated with the sensors and other application specific devices through its GPIOs with minimal development up-front and minimal loading during run - time. Its high degree of on-chip integration allows for minimal external circuitry, including the0front-end module, is designed to occupy minimal PCB area. The ESP8266 supports APSD for VoIP ...