Arduino – Home

Arduino Tutorial PDF Version Quick Guide Resources Job Search Discussion Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists of a circuit board, which can be programed (referred to as a microcontroller) and a ready-made software called Arduino IDE (Integrated Development Environment), which is used to write and upload the computer code to the physical board. Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package. Audience This tutorial is intended for enthusiastic students or hobbyists. With Arduino, one can get to know the basics of micro-controllers and sensors very quickly and can start building prototype with very little investment. This tutorial is intended to make you comfortable in getting started with Arduino and its various functions. Prerequisites Before you start proceeding with this tutorial, we assume that you are already familiar with the basics of C and C++. If you are not well aware of these concepts, then we will suggest you go through our short tutorials on C and C++. A basic understanding of microcontrollers and electronics is also expected. Print Page Previous Next Advertisements ”;

Arduino – Board Description

Arduino – Board Description ”; Previous Next In this chapter, we will learn about the different components on the Arduino board. We will study the Arduino UNO board because it is the most popular board in the Arduino board family. In addition, it is the best board to get started with electronics and coding. Some boards look a bit different from the one given below, but most Arduinos have majority of these components in common. Power USB Arduino board can be powered by using the USB cable from your computer. All you need to do is connect the USB cable to the USB connection (1). Power (Barrel Jack) Arduino boards can be powered directly from the AC mains power supply by connecting it to the Barrel Jack (2). Voltage Regulator The function of the voltage regulator is to control the voltage given to the Arduino board and stabilize the DC voltages used by the processor and other elements. Crystal Oscillator The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate time? The answer is, by using the crystal oscillator. The number printed on top of the Arduino crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz. Arduino Reset You can reset your Arduino board, i.e., start your program from the beginning. You can reset the UNO board in two ways. First, by using the reset button (17) on the board. Second, you can connect an external reset button to the Arduino pin labelled RESET (5). Pins (3.3, 5, GND, Vin) 3.3V (6) − Supply 3.3 output volt 5V (7) − Supply 5 output volt Most of the components used with Arduino board works fine with 3.3 volt and 5 volt. GND (8)(Ground) − There are several GND pins on the Arduino, any of which can be used to ground your circuit. Vin (9) − This pin also can be used to power the Arduino board from an external power source, like AC mains power supply. Analog pins The Arduino UNO board has six analog input pins A0 through A5. These pins can read the signal from an analog sensor like the humidity sensor or temperature sensor and convert it into a digital value that can be read by the microprocessor. Main microcontroller Each Arduino board has its own microcontroller (11). You can assume it as the brain of your board. The main IC (integrated circuit) on the Arduino is slightly different from board to board. The microcontrollers are usually of the ATMEL Company. You must know what IC your board has before loading up a new program from the Arduino IDE. This information is available on the top of the IC. For more details about the IC construction and functions, you can refer to the data sheet. ICSP pin Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral Interface), which could be considered as an “expansion” of the output. Actually, you are slaving the output device to the master of the SPI bus. Power LED indicator This LED should light up when you plug your Arduino into a power source to indicate that your board is powered up correctly. If this light does not turn on, then there is something wrong with the connection. TX and RX LEDs On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate the pins responsible for serial communication. Second, the TX and RX led (13). The TX led flashes with different speed while sending the serial data. The speed of flashing depends on the baud rate used by the board. RX flashes during the receiving process. Digital I/O The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse Width Modulation) output. These pins can be configured to work as input digital pins to read logic values (0 or 1) or as digital output pins to drive different modules like LEDs, relays, etc. The pins labeled “~” can be used to generate PWM. AREF AREF stands for Analog Reference. It is sometimes, used to set an external reference voltage (between 0 and 5 Volts) as the upper limit for the analog input pins. Print Page Previous Next Advertisements ”;

Arduino – Strings

Arduino – Strings ”; Previous Next Strings are used to store text. They can be used to display text on an LCD or in the Arduino IDE Serial Monitor window. Strings are also useful for storing the user input. For example, the characters that a user types on a keypad connected to the Arduino. There are two types of strings in Arduino programming − Arrays of characters, which are the same as the strings used in C programming. The Arduino String, which lets us use a string object in a sketch. In this chapter, we will learn Strings, objects and the use of strings in Arduino sketches. By the end of the chapter, you will learn which type of string to use in a sketch. String Character Arrays The first type of string that we will learn is the string that is a series of characters of the type char. In the previous chapter, we learned what an array is; a consecutive series of the same type of variable stored in memory. A string is an array of char variables. A string is a special array that has one extra element at the end of the string, which always has the value of 0 (zero). This is known as a “null terminated string”. String Character Array Example This example will show how to make a string and print it to the serial monitor window. Example void setup() { char my_str[6]; // an array big enough for a 5 character string Serial.begin(9600); my_str[0] = ”H”; // the string consists of 5 characters my_str[1] = ”e”; my_str[2] = ”l”; my_str[3] = ”l”; my_str[4] = ”o”; my_str[5] = 0; // 6th array element is a null terminator Serial.println(my_str); } void loop() { } The following example shows what a string is made up of; a character array with printable characters and 0 as the last element of the array to show that this is where the string ends. The string can be printed out to the Arduino IDE Serial Monitor window by using Serial.println() and passing the name of the string. This same example can be written in a more convenient way as shown below − Example void setup() { char my_str[] = “Hello”; Serial.begin(9600); Serial.println(my_str); } void loop() { } In this sketch, the compiler calculates the size of the string array and also automatically null terminates the string with a zero. An array that is six elements long and consists of five characters followed by a zero is created exactly the same way as in the previous sketch. Manipulating String Arrays We can alter a string array within a sketch as shown in the following sketch. Example void setup() { char like[] = “I like coffee and cake”; // create a string Serial.begin(9600); // (1) print the string Serial.println(like); // (2) delete part of the string like[13] = 0; Serial.println(like); // (3) substitute a word into the string like[13] = ” ”; // replace the null terminator with a space like[18] = ”t”; // insert the new word like[19] = ”e”; like[20] = ”a”; like[21] = 0; // terminate the string Serial.println(like); } void loop() { } Result I like coffee and cake I like coffee I like coffee and tea The sketch works in the following way. Creating and Printing the String In the sketch given above, a new string is created and then printed for display in the Serial Monitor window. Shortening the String The string is shortened by replacing the 14th character in the string with a null terminating zero (2). This is element number 13 in the string array counting from 0. When the string is printed, all the characters are printed up to the new null terminating zero. The other characters do not disappear; they still exist in the memory and the string array is still the same size. The only difference is that any function that works with strings will only see the string up to the first null terminator. Changing a Word in the String Finally, the sketch replaces the word “cake” with “tea” (3). It first has to replace the null terminator at like[13] with a space so that the string is restored to the originally created format. New characters overwrite “cak” of the word “cake” with the word “tea”. This is done by overwriting individual characters. The ”e” of “cake” is replaced with a new null terminating character. The result is that the string is actually terminated with two null characters, the original one at the end of the string and the new one that replaces the ”e” in “cake”. This makes no difference when the new string is printed because the function that prints the string stops printing the string characters when it encounters the first null terminator. Functions to Manipulate String Arrays The previous sketch manipulated the string in a manual way by accessing individual characters in the string. To make it easier to manipulate string arrays, you can write your own functions to do so, or use some of the string functions from the C language library. Given below is the list Functions to Manipulate String Arrays S.No. Functions & Description 1 String() The String class, part of the core as of version 0019, allows you to use and manipulate strings of text in more complex ways than character arrays do. You can concatenate Strings, append to them, search for and replace substrings, and more. It takes more memory than a simple character array, but it is also more useful. For reference, character arrays are referred to as strings with a small ‘s’, and instances of the String class are referred to as Strings with a capital S. Note that constant strings, specified in “double quotes” are treated as char arrays, not instances of the String class 2 charAt() Access a particular character of the String. 3 compareTo() Compares two Strings, testing whether one comes before or after the other, or whether they are equal.

Arduino – Variables & Constants

Arduino – Variables & Constants ”; Previous Next Before we start explaining the variable types, a very important subject we need to make sure, you fully understand is called the variable scope. What is Variable Scope? Variables in C programming language, which Arduino uses, have a property called scope. A scope is a region of the program and there are three places where variables can be declared. They are − Inside a function or a block, which is called local variables. In the definition of function parameters, which is called formal parameters. Outside of all functions, which is called global variables. Local Variables Variables that are declared inside a function or block are local variables. They can be used only by the statements that are inside that function or block of code. Local variables are not known to function outside their own. Following is the example using local variables − Void setup () { } Void loop () { int x , y ; int z ; Local variable declaration x = 0; y = 0; actual initialization z = 10; } Global Variables Global variables are defined outside of all the functions, usually at the top of the program. The global variables will hold their value throughout the life-time of your program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following example uses global and local variables − Int T , S ; float c = 0 ; Global variable declaration Void setup () { } Void loop () { int x , y ; int z ; Local variable declaration x = 0; y = 0; actual initialization z = 10; } Print Page Previous Next Advertisements ”;

Arduino – Program Structure

Arduino – Program Structure ”; Previous Next In this chapter, we will study in depth, the Arduino program structure and we will learn more new terminologies used in the Arduino world. The Arduino software is open-source. The source code for the Java environment is released under the GPL and the C/C++ microcontroller libraries are under the LGPL. Sketch − The first new terminology is the Arduino program called “sketch”. Structure Arduino programs can be divided in three main parts: Structure, Values (variables and constants), and Functions. In this tutorial, we will learn about the Arduino software program, step by step, and how we can write the program without any syntax or compilation error. Let us start with the Structure. Software structure consist of two main functions − Setup( ) function Loop( ) function Void setup ( ) { } PURPOSE − The setup() function is called when a sketch starts. Use it to initialize the variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the Arduino board. INPUT − – OUTPUT − – RETURN − – Void Loop ( ) { } PURPOSE − After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board. INPUT − – OUTPUT − – RETURN − – Print Page Previous Next Advertisements ”;

Arduino – Control Statements

Arduino – Control Statements ”; Previous Next Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program. It should be along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Following is the general form of a typical decision making structure found in most of the programming languages − Control Statements are elements in Source Code that control the flow of program execution. They are − S.NO. Control Statement & Description 1 If statement It takes an expression in parenthesis and a statement or block of statements. If the expression is true then the statement or block of statements gets executed otherwise these statements are skipped. 2 If …else statement An if statement can be followed by an optional else statement, which executes when the expression is false. 3 If…else if …else statement The if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement. 4 switch case statement Similar to the if statements, switch…case controls the flow of programs by allowing the programmers to specify different codes that should be executed in various conditions. 5 Conditional Operator ? : The conditional operator ? : is the only ternary operator in C. Print Page Previous Next Advertisements ”;

Arduino – I/O Functions

Arduino – I/O Functions ”; Previous Next The pins on the Arduino board can be configured as either inputs or outputs. We will explain the functioning of the pins in those modes. It is important to note that a majority of Arduino analog pins, may be configured, and used, in exactly the same manner as digital pins. Pins Configured as INPUT Arduino pins are by default configured as inputs, so they do not need to be explicitly declared as inputs with pinMode() when you are using them as inputs. Pins configured this way are said to be in a high-impedance state. Input pins make extremely small demands on the circuit that they are sampling, equivalent to a series resistor of 100 megaohm in front of the pin. This means that it takes very little current to switch the input pin from one state to another. This makes the pins useful for such tasks as implementing a capacitive touch sensor or reading an LED as a photodiode. Pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires connected to them that are not connected to other circuits, report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin. Pull-up Resistors Pull-up resistors are often useful to steer an input pin to a known state if no input is present. This can be done by adding a pull-up resistor (to +5V), or a pull-down resistor (resistor to ground) on the input. A 10K resistor is a good value for a pull-up or pull-down resistor. Using Built-in Pull-up Resistor with Pins Configured as Input There are 20,000 pull-up resistors built into the Atmega chip that can be accessed from software. These built-in pull-up resistors are accessed by setting the pinMode() as INPUT_PULLUP. This effectively inverts the behavior of the INPUT mode, where HIGH means the sensor is OFF and LOW means the sensor is ON. The value of this pull-up depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino Due, it is between 50kΩ and 150kΩ. For the exact value, consult the datasheet of the microcontroller on your board. When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be connected to the ground. In case of a simple switch, this causes the pin to read HIGH when the switch is open and LOW when the switch is pressed. The pull-up resistors provide enough current to light an LED dimly connected to a pin configured as an input. If LEDs in a project seem to be working, but very dimly, this is likely what is going on. Same registers (internal chip memory locations) that control whether a pin is HIGH or LOW control the pull-up resistors. Consequently, a pin that is configured to have pull-up resistors turned on when the pin is in INPUTmode, will have the pin configured as HIGH if the pin is then switched to an OUTPUT mode with pinMode(). This works in the other direction as well, and an output pin that is left in a HIGH state will have the pull-up resistor set if switched to an input with pinMode(). Example pinMode(3,INPUT) ; // set pin to input without using built in pull up resistor pinMode(5,INPUT_PULLUP) ; // set pin to input using built in pull up resistor Pins Configured as OUTPUT Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. Atmega pins can source (provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current to other devices/circuits. This is enough current to brightly light up an LED (do not forget the series resistor), or run many sensors but not enough current to run relays, solenoids, or motors. Attempting to run high current devices from the output pins, can damage or destroy the output transistors in the pin, or damage the entire Atmega chip. Often, this results in a “dead” pin in the microcontroller but the remaining chips still function adequately. For this reason, it is a good idea to connect the OUTPUT pins to other devices through 470Ω or 1k resistors, unless maximum current drawn from the pins is required for a particular application. pinMode() Function The pinMode() function is used to configure a specific pin to behave either as an input or an output. It is possible to enable the internal pull-up resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pull-ups. pinMode() Function Syntax Void setup () { pinMode (pin , mode); } pin − the number of the pin whose mode you wish to set mode − INPUT, OUTPUT, or INPUT_PULLUP. Example int button = 5 ; // button connected to pin 5 int LED = 6; // LED connected to pin 6 void setup () { pinMode(button , INPUT_PULLUP); // set the digital pin as input with pull-up resistor pinMode(button , OUTPUT); // set the digital pin as output } void setup () { If (digitalRead(button ) == LOW) // if button pressed { digitalWrite(LED,HIGH); // turn on led delay(500); // delay for 500 ms digitalWrite(LED,LOW); // turn off led delay(500); // delay for 500 ms } } digitalWrite() Function The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. If you do not set the pinMode() to OUTPUT, and connect an LED

Arduino – Character Functions

Arduino – Character Functions ”; Previous Next All data is entered into computers as characters, which includes letters, digits and various special symbols. In this section, we discuss the capabilities of C++ for examining and manipulating individual characters. The character-handling library includes several functions that perform useful tests and manipulations of character data. Each function receives a character, represented as an int, or EOF as an argument. Characters are often manipulated as integers. Remember that EOF normally has the value –1 and that some hardware architectures do not allow negative values to be stored in char variables. Therefore, the character-handling functions manipulate characters as integers. The following table summarizes the functions of the character-handling library. When using functions from the character-handling library, include the <cctype> header. S.No. Prototype & Description 1 int isdigit( int c ) Returns 1 if c is a digit and 0 otherwise. 2 int isalpha( int c ) Returns 1 if c is a letter and 0 otherwise. 3 int isalnum( int c ) Returns 1 if c is a digit or a letter and 0 otherwise. 4 int isxdigit( int c ) Returns 1 if c is a hexadecimal digit character and 0 otherwise. (See Appendix D, Number Systems, for a detailed explanation of binary, octal, decimal and hexadecimal numbers.) 5 int islower( int c ) Returns 1 if c is a lowercase letter and 0 otherwise. 6 int isupper( int c ) Returns 1 if c is an uppercase letter; 0 otherwise. 7 int isspace( int c ) Returns 1 if c is a white-space character—newline (”n”), space (” ”), form feed (”f”), carriage return (”r”), horizontal tab (”t”), or vertical tab (”v”)—and 0 otherwise. 8 int iscntrl( int c ) Returns 1 if c is a control character, such as newline (”n”), form feed (”f”), carriage return (”r”), horizontal tab (”t”), vertical tab (”v”), alert (”a”), or backspace (”b”)—and 0 otherwise. 9 int ispunct( int c ) Returns 1 if c is a printing character other than a space, a digit, or a letter and 0 otherwise. 10 int isprint( int c ) Returns 1 if c is a printing character including space (” ”) and 0 otherwise. 11 int isgraph( int c ) Returns 1 if c is a printing character other than space (” ”) and 0 otherwise. Examples The following example demonstrates the use of the functions isdigit, isalpha, isalnum and isxdigit. Function isdigit determines whether its argument is a digit (0–9). The function isalpha determines whether its argument is an uppercase letter (A-Z) or a lowercase letter (a–z). The function isalnum determines whether its argument is an uppercase, lowercase letter or a digit. Function isxdigit determines whether its argument is a hexadecimal digit (A–F, a–f, 0–9). Example 1 void setup () { Serial.begin (9600); Serial.print (“According to isdigit:r”); Serial.print (isdigit( ”8” ) ? “8 is a”: “8 is not a”); Serial.print (” digitr” ); Serial.print (isdigit( ”8” ) ?”# is a”: “# is not a”) ; Serial.print (” digitr”); Serial.print (“rAccording to isalpha:r” ); Serial.print (isalpha(”A” ) ?”A is a”: “A is not a”); Serial.print (” letterr”); Serial.print (isalpha(”A” ) ?”b is a”: “b is not a”); Serial.print (” letterr”); Serial.print (isalpha(”A”) ?”& is a”: “& is not a”); Serial.print (” letterr”); Serial.print (isalpha( ”A” ) ?”4 is a”:”4 is not a”); Serial.print (” letterr”); Serial.print (“rAccording to isalnum:r”); Serial.print (isalnum( ”A” ) ?”A is a” : “A is not a” ); Serial.print (” digit or a letterr” ); Serial.print (isalnum( ”8” ) ?”8 is a” : “8 is not a” ) ; Serial.print (” digit or a letterr”); Serial.print (isalnum( ”#” ) ?”# is a” : “# is not a” ); Serial.print (” digit or a letterr”); Serial.print (“rAccording to isxdigit:r”); Serial.print (isxdigit( ”F” ) ?”F is a” : “F is not a” ); Serial.print (” hexadecimal digitr” ); Serial.print (isxdigit( ”J” ) ?”J is a” : “J is not a” ) ; Serial.print (” hexadecimal digitr” ); Serial.print (isxdigit( ”7” ) ?”7 is a” : “7 is not a” ) ; Serial.print (” hexadecimal digitr” ); Serial.print (isxdigit( ”$” ) ? “$ is a” : “$ is not a” ); Serial.print (” hexadecimal digitr” ); Serial.print (isxdigit( ”f” ) ? “f is a” : “f is not a”); } void loop () { } Result According to isdigit: 8 is a digit # is not a digit According to isalpha: A is a letter b is a letter & is not a letter 4 is not a letter According to isalnum: A is a digit or a letter 8 is a digit or a letter # is not a digit or a letter According to isxdigit: F is a hexadecimal digit J is not a hexadecimal digit 7 is a hexadecimal digit $ is not a hexadecimal digit f is a hexadecimal digit We use the conditional operator (?:) with each function to determine whether the string ” is a ” or the string ” is not a ” should be printed in the output for each character tested. For example, line a indicates that if ”8” is a digit—i.e., if isdigit returns a true (nonzero) value—the string “8 is a ” is printed. If ”8” is not a digit (i.e., if isdigit returns 0), the string ” 8 is not a ” is printed. Example 2 The following example demonstrates the use of the functions islower and isupper. The function islower determines whether its argument is a lowercase letter (a–z). Function isupper determines whether its argument is an uppercase letter (A–Z). int thisChar = 0xA0; void setup () { Serial.begin (9600); Serial.print (“According to islower:r”) ; Serial.print (islower( ”p” ) ? “p is a” : “p is not a” ); Serial.print ( ” lowercase letterr” ); Serial.print ( islower( ”P”) ? “P is a” : “P is not a”) ; Serial.print (“lowercase letterr”); Serial.print (islower( ”5” ) ? “5 is a” : “5 is not a” ); Serial.print ( ” lowercase letterr”

Arduino – Installation

Arduino – Installation ”; Previous Next After learning about the main parts of the Arduino UNO board, we are ready to learn how to set up the Arduino IDE. Once we learn this, we will be ready to upload our program on the Arduino board. In this section, we will learn in easy steps, how to set up the Arduino IDE on our computer and prepare the board to receive the program via USB cable. Step 1 − First you must have your Arduino board (you can choose your favorite board) and a USB cable. In case you use Arduino UNO, Arduino Duemilanove, Nano, Arduino Mega 2560, or Diecimila, you will need a standard USB cable (A plug to B plug), the kind you would connect to a USB printer as shown in the following image. In case you use Arduino Nano, you will need an A to Mini-B cable instead as shown in the following image. Step 2 − Download Arduino IDE Software. You can get different versions of Arduino IDE from the Download page on the Arduino Official website. You must select your software, which is compatible with your operating system (Windows, IOS, or Linux). After your file download is complete, unzip the file. Step 3 − Power up your board. The Arduino Uno, Mega, Duemilanove and Arduino Nano automatically draw power from either, the USB connection to the computer or an external power supply. If you are using an Arduino Diecimila, you have to make sure that the board is configured to draw power from the USB connection. The power source is selected with a jumper, a small piece of plastic that fits onto two of the three pins between the USB and power jacks. Check that it is on the two pins closest to the USB port. Connect the Arduino board to your computer using the USB cable. The green power LED (labeled PWR) should glow. Step 4 − Launch Arduino IDE. After your Arduino IDE software is downloaded, you need to unzip the folder. Inside the folder, you can find the application icon with an infinity label (application.exe). Double-click the icon to start the IDE. Step 5 − Open your first project. Once the software starts, you have two options − Create a new project. Open an existing project example. To create a new project, select File → New. To open an existing project example, select File → Example → Basics → Blink. Here, we are selecting just one of the examples with the name Blink. It turns the LED on and off with some time delay. You can select any other example from the list. Step 6 − Select your Arduino board. To avoid any error while uploading your program to the board, you must select the correct Arduino board name, which matches with the board connected to your computer. Go to Tools → Board and select your board. Here, we have selected Arduino Uno board according to our tutorial, but you must select the name matching the board that you are using. Step 7 − Select your serial port. Select the serial device of the Arduino board. Go to Tools → Serial Port menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your Arduino board and re-open the menu, the entry that disappears should be of the Arduino board. Reconnect the board and select that serial port. Step 8 − Upload the program to your board. Before explaining how we can upload our program to the board, we must demonstrate the function of each symbol appearing in the Arduino IDE toolbar. A − Used to check if there is any compilation error. B − Used to upload a program to the Arduino board. C − Shortcut used to create a new sketch. D − Used to directly open one of the example sketch. E − Used to save your sketch. F − Serial monitor used to receive serial data from the board and send the serial data to the board. Now, simply click the “Upload” button in the environment. Wait a few seconds; you will see the RX and TX LEDs on the board, flashing. If the upload is successful, the message “Done uploading” will appear in the status bar. Note − If you have an Arduino Mini, NG, or other board, you need to press the reset button physically on the board, immediately before clicking the upload button on the Arduino Software. Print Page Previous Next Advertisements ”;

Arduino – Data Types

Arduino – Data Types ”; Previous Next Data types in C refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in the storage and how the bit pattern stored is interpreted. The following table provides all the data types that you will use during Arduino programming. void Boolean char Unsigned char byte int Unsigned int word long Unsigned long short float double array String-char array String-object void The void keyword is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called. Example Void Loop ( ) { // rest of the code } Boolean A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte of memory. Example boolean val = false ; // declaration of variable with type boolean and initialize it with false boolean state = true ; // declaration of variable with type boolean and initialize it with true Char A data type that takes up one byte of memory that stores a character value. Character literals are written in single quotes like this: ”A” and for multiple characters, strings use double quotes: “ABC”. However, characters are stored as numbers. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic operations on characters, in which the ASCII value of the character is used. For example, ”A” + 1 has the value 66, since the ASCII value of the capital letter A is 65. Example Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it with character a Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97 unsigned char Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255. Example Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y byte A byte stores an 8-bit unsigned number, from 0 to 255. Example byte m = 25 ;//declaration of variable with type byte and initialize it with 25 int Integers are the primary data-type for number storage. int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) – 1). The int size varies from board to board. On the Arduino Due, for example, an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) – 1). Example int counter = 32 ;// declaration of variable with type int and initialize it with 32 Unsigned int Unsigned ints (unsigned integers) are the same as int in the way that they store a 2 byte value. Instead of storing negative numbers, however, they only store positive values, yielding a useful range of 0 to 65,535 (2^16) – 1). The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32 – 1). Example Unsigned int counter = 60 ; // declaration of variable with type unsigned int and initialize it with 60 Word On the Uno and other ATMEGA based boards, a word stores a 16-bit unsigned number. On the Due and Zero, it stores a 32-bit unsigned number. Example word w = 1000 ;//declaration of variable with type word and initialize it with 1000 Long Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647. Example Long velocity = 102346 ;//declaration of variable with type Long and initialize it with 102346 unsigned long Unsigned long variables are extended size variables for number storage and store 32 bits (4 bytes). Unlike standard longs, unsigned longs will not store negative numbers, making their range from 0 to 4,294,967,295 (2^32 – 1). Example Unsigned Long velocity = 101006 ;// declaration of variable with type Unsigned Long and initialize it with 101006 short A short is a 16-bit data-type. On all Arduinos (ATMega and ARM based), a short stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) – 1). Example short val = 13 ;//declaration of variable with type short and initialize it with 13 float Data type for floating-point number is a number that has a decimal point. Floating-point numbers are often used to approximate the analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information. Example float num = 1.352;//declaration of variable with type float and initialize it with 1.352 double On the Uno and other ATMEGA based boards, Double precision floating-point number occupies four bytes. That is, the double implementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision. Example double num = 45.352 ;// declaration of variable with type double and initialize it with 45.352 Print Page Previous Next Advertisements ”;