GETTING STARTED WITH ARDUINO IDE
Before you can start doing anything with the Arduino, you need to download and install the Arduino IDE (integrated development environment). From this point on we will be referring to the Arduino IDE as the Arduino Programmer.
The Arduino Programmer is based on the Processing IDE and uses a variation of the C and C++ programming languages.
You can find the most recent version of the Arduino Programmer on this page.
Plug it in
Connect the Arduino to your computer's USB port.
Please note that although the Arduino plugs into your computer, it is not a true USB device. The board has a special chip that allows it to show up on your computer as a virtual serial port when it is plugged into a USB port. This is why it is important to plug the board in. When the board is not plugged in, the virtual serial port that the Arduino operates upon will not be present (since all of the information about it lives on the Arduino board).
It is also good to know that every single Arduino has a unique virtual serial port address. This means that every time you plug in a different Arduino board into your computer, you will need to reconfigure the serial port that is in use.
The Arduino Uno requires a male USB A to male USB B cable.
Please note that although the Arduino plugs into your computer, it is not a true USB device. The board has a special chip that allows it to show up on your computer as a virtual serial port when it is plugged into a USB port. This is why it is important to plug the board in. When the board is not plugged in, the virtual serial port that the Arduino operates upon will not be present (since all of the information about it lives on the Arduino board).
It is also good to know that every single Arduino has a unique virtual serial port address. This means that every time you plug in a different Arduino board into your computer, you will need to reconfigure the serial port that is in use.
The Arduino Uno requires a male USB A to male USB B cable.
Settings
Before you can start doing anything in the Arduino programmer, you must set the board-type and serial port.
To set the board, go to the following:
Select the version of board that you are using. Since I have an Arduino Uno plugged in, I obviously selected "Arduino Uno."
To set the serial port, go to the following:
Select the serial port that looks like:
To set the board, go to the following:
Tools --> Boards
Select the version of board that you are using. Since I have an Arduino Uno plugged in, I obviously selected "Arduino Uno."
To set the serial port, go to the following:
Tools --> Serial Port
Select the serial port that looks like:
/dev/tty.usbmodem [random numbers]
Arduino programs are called sketches. The Arduino programmer comes with a ton of example sketches preloaded. This is great because even if you have never programmed anything in your life, you can load one of these sketches and get the Arduino to do something.
To get the LED tied to digital pin 13 to blink on and off, let's load the blink example.
The blink example can be found here:
The blink example basically sets pin D13 as an output and then blinks the test LED on the Arduino board on and off every second.
Once the blink example is open, it can be installed onto the ATMEGA328 chip by pressing the upload button, which looks like an arrow pointing to the right.
Notice that the surface mount status LED connected to pin 13 on the Arduino will start to blink. You can change the rate of the blinking by changing the length of the delay and pressing the upload button again.
To get the LED tied to digital pin 13 to blink on and off, let's load the blink example.
The blink example can be found here:
Files --> Examples --> Basics --> Blink
The blink example basically sets pin D13 as an output and then blinks the test LED on the Arduino board on and off every second.
Once the blink example is open, it can be installed onto the ATMEGA328 chip by pressing the upload button, which looks like an arrow pointing to the right.
Notice that the surface mount status LED connected to pin 13 on the Arduino will start to blink. You can change the rate of the blinking by changing the length of the delay and pressing the upload button again.
The serial monitor allows your computer to connect serially with the Arduino. This is important because it takes data that your Arduino is receiving from sensors and other devices and displays it in real-time on your computer. Having this ability is invaluable to debug your code and understand what number values the chip is actually receiving.
For instance, connect center sweep (middle pin) of a potentiometer to A0, and the outer pins, respectively, to 5v and ground. Next upload the sketch shown below:
Click the button to engage the serial monitor which looks like a magnifying glass. You can now see the numbers being read by the analog pin in the serial monitor. When you turn the knob the numbers will increase and decrease.
The numbers will be between the range of 0 and 1023. The reason for this is that the analog pin is converting a voltage between 0 and 5V to a discreet number.
For instance, connect center sweep (middle pin) of a potentiometer to A0, and the outer pins, respectively, to 5v and ground. Next upload the sketch shown below:
File --> Examples --> 1.Basics --> AnalogReadSerial
Click the button to engage the serial monitor which looks like a magnifying glass. You can now see the numbers being read by the analog pin in the serial monitor. When you turn the knob the numbers will increase and decrease.
The numbers will be between the range of 0 and 1023. The reason for this is that the analog pin is converting a voltage between 0 and 5V to a discreet number.
The Arduino has two different types of input pins, those being analog and digital.
To begin with, lets look at the digital input pins.
Digital input pins only have two possible states, which are on or off. These two on and off states are also referred to as:
Digital inputs can also be used as the basis for countless digital communication protocols. By creating a 5V (HIGH) pulse or 0V (LOW) pulse, you can create a binary signal, the basis of all computing. This is useful for talking to digital sensors like a PING ultrasonic sensor, or communicating with other devices.
For a simple example of a digital input in use, connect a switch from digital pin 2 to 5V, a 10K resistor** from digital pin 2 to ground, and run the following code:
**The 10K resistor is called a pull-down resistor because it connects the digital pin to ground when the switch is not pressed. When the switch is pressed, the electrical connections in the switch has less resistance than the resistor, and the electricity no longer connects to ground. Instead, electricity flows between 5V and the digital pin. This is because electricity always chooses the path of least resistance. To learn more about this, visit the Digital Pins page.
To begin with, lets look at the digital input pins.
Digital input pins only have two possible states, which are on or off. These two on and off states are also referred to as:
- HIGH or LOW
- 1 or 0
- 5V or 0V.
Digital inputs can also be used as the basis for countless digital communication protocols. By creating a 5V (HIGH) pulse or 0V (LOW) pulse, you can create a binary signal, the basis of all computing. This is useful for talking to digital sensors like a PING ultrasonic sensor, or communicating with other devices.
For a simple example of a digital input in use, connect a switch from digital pin 2 to 5V, a 10K resistor** from digital pin 2 to ground, and run the following code:
File --> Examples --> 2.Digital --> Button
**The 10K resistor is called a pull-down resistor because it connects the digital pin to ground when the switch is not pressed. When the switch is pressed, the electrical connections in the switch has less resistance than the resistor, and the electricity no longer connects to ground. Instead, electricity flows between 5V and the digital pin. This is because electricity always chooses the path of least resistance. To learn more about this, visit the Digital Pins page.
Aside from the digital input pins, the Arduino also boasts a number of analog input pins.
Analog input pins take an analog signal and perform a 10-bit analog-to-digital (ADC) conversion to turn it into a number between 0 and 1023 (4.9mV steps).
This type of input is good for reading resistive sensors. These are basically sensors which provide resistance to the circuit. They are also good for reading a varying voltage signal between 0 and 5V. This is useful when interfacing with various types of analog circuitry.
If you followed the example in Step 7 for engaging the serial monitor, you have already tried using an analog input pin.
Analog input pins take an analog signal and perform a 10-bit analog-to-digital (ADC) conversion to turn it into a number between 0 and 1023 (4.9mV steps).
This type of input is good for reading resistive sensors. These are basically sensors which provide resistance to the circuit. They are also good for reading a varying voltage signal between 0 and 5V. This is useful when interfacing with various types of analog circuitry.
If you followed the example in Step 7 for engaging the serial monitor, you have already tried using an analog input pin.
A digital out pin can be set to be HIGH (5v) or LOW (0v). This allows you to turn things on and off.
Aside from turning things on and off (and making LEDs blink), this form of output is convenient for a number of applications.
Most notably, it allows you to communicate digitally. By turning the pin on and off rapidly, you are creating binary states (0 and 1), which is recognized by countless other electronic devices as a binary signal. By using this method, you can communicate using a number of different protocols.
Digital communication is an advanced topic, but to get a general idea of what can be done, check out the Interfacing With Hardware page.
If you followed the example in Step 6 for getting an LED to blink, you have already tried using a digital output pin.
Aside from turning things on and off (and making LEDs blink), this form of output is convenient for a number of applications.
Most notably, it allows you to communicate digitally. By turning the pin on and off rapidly, you are creating binary states (0 and 1), which is recognized by countless other electronic devices as a binary signal. By using this method, you can communicate using a number of different protocols.
Digital communication is an advanced topic, but to get a general idea of what can be done, check out the Interfacing With Hardware page.
If you followed the example in Step 6 for getting an LED to blink, you have already tried using a digital output pin.
As mentioned earlier, the Arduino has a number of built in special functions. One of these special functions is pulse width modulation, which is the way an Arduino is able to create an analog-like output.
Pulse width modulation - or PWM for short - works by rapidly turning the PWM pin high (5V) and low (0V) to simulate an analog signal. For instance, if you were to blink an LED on and off rapidly enough (about five milliseconds each), it would seem to average the brightness and only appear to be receiving half the power. Alternately, if it were to blink on for 1 millisecond and then blink off for 9 millisecond, the LED would appear to be 1/10 as bright and only be receiving 1/10 the voltage.
PWM is key for a number of applications including making sound, controlling the brightness of lights, and controlling the speed of motors.
For a more in-depth explanation, check out secrets of PWM page.
To try out PWM yourself, connect an LED and 220 ohm resistor to digital pin 9, in series to ground. Run the following example code:
Pulse width modulation - or PWM for short - works by rapidly turning the PWM pin high (5V) and low (0V) to simulate an analog signal. For instance, if you were to blink an LED on and off rapidly enough (about five milliseconds each), it would seem to average the brightness and only appear to be receiving half the power. Alternately, if it were to blink on for 1 millisecond and then blink off for 9 millisecond, the LED would appear to be 1/10 as bright and only be receiving 1/10 the voltage.
PWM is key for a number of applications including making sound, controlling the brightness of lights, and controlling the speed of motors.
For a more in-depth explanation, check out secrets of PWM page.
To try out PWM yourself, connect an LED and 220 ohm resistor to digital pin 9, in series to ground. Run the following example code:
File --> Examples --> 3.Analog --> Fading
To write your own code, you will need to learn some basic programming language syntax. In other words, you have to learn how to properly form the code for the programmer to understand it. You can think of this kind of like understanding grammar and punctuation. You can write an entire book without proper grammar and punctuation, but no one will be abler to understand it, even if it is in English.
Some important things to keep in mind when writing your own code:
- An Arduino program is called a sketch.
- All code in an Arduino sketch is processed from top to bottom.
- Arduino sketches are typically broken into five parts.
- The sketch usually starts with a header that explains what the sketch is doing, and who wrote it.
- Next, it usually defines global variables. Often, this is where constant names are given to the different Arduino pins.
- After the initial variables are set, the Arduino begins the setup routine. In the setup function, we set initial conditions of variables when necessary, and run any preliminary code that we only want to run once. This is where serial communication is initiated, which is required for running the serial monitor.
- From the setup function, we go to the loop routine. This is the main routine of the sketch. This is not only where your main code goes, but it will be executed over and over, so long as the sketch continues to run.
- Below the loop routine, there is often other functions listed. These functions are user-defined and only activated when called in the setup and loop routine. When these functions are called, the Arduino processes all of the code in the function from top to bottom and then goes back to the next line in the sketch where it left off when the function was called. Functions are good because they allow you to run standard routines - over and over - without having to write the same lines of code over and over. You can simply call upon a function multiple times, and this will free up memory on the chip because the function routine is only written once. It also makes code easier to read. To learn how to form your own functions, check outthis page.
- All of that said, the only two parts of the sketch which are mandatory are the Setup and Loop routines.
- Code must be written in the Arduino Language, which is roughly based on C.
- Almost all statements written in the Arduino language must end with a ;
- Conditionals (such as if statements and for loops) do not need a ;
- Conditionals have their own rules and can be found under "Control Structures" on the Arduino Language page
- Variables are storage compartments for numbers. You can pass values into and out of variables. Variables must be defined (stated in the code) before they can be used and need to have a data type associated with it. To learn some of the basic data types, review the Language Page.
Okay! So let us say we want to write code that reads a photocell connected to pin A0, and use the reading we get from the photocell to control the brightness of an LED connected to pin D9.
First, we want to open the BareMinimum sketch, which can be found at:
File --> Examples --> 1.Basic --> BareMinimum
The BareMinimum Sketch should look like this:<pre>void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: }
Next, lets put a header on the code, so other people know about what we are making, why, and under what terms:
<pre>/* LED Dimmer by Genius Arduino Programmer 2012 Controls the brightness of an LED on pin D9 based on the reading of a photocell on pin A0 This code is in the Public Domain */ void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: }
Once that is all squared away, let us define the pin names, and establish variables:
<pre>/* LED Dimmer by Genius Arduino Programmer 2012 Controls the brightness of an LED on pin D9 based on the reading of a photocell on pin A0 This code is in the Public Domain */ // name analog pin 0 a constant name const int analogInPin = A0; // name digital pin 9 a constant name const int LEDPin = 9; //variable for reading a photocell int photocell; void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: }
Now that variables and pin names are set, let us write the actual code:
<pre>/* LED Dimmer by Genius Arduino Programmer 2012 Controls the brightness of an LED on pin D9 based on the reading of a photocell on pin A0 This code is in the Public Domain */ // name analog pin 0 a constant name const int analogInPin = A0; // name digital pin 9 a constant name const int LEDPin = 9; //variable for reading a photocell int photocell; void setup() { //nothing here right now } void loop() { //read the analog in pin and set the reading to the photocell variable photocell = analogRead(analogInPin); //control the LED pin using the value read by the photocell analogWrite(LEDPin, photocell); //pause the code for 1/10 second //1 second = 1000 delay(100); }
If we want to see what numbers the analog pin is actually reading from the photocell, we will need to use the serial monitor. Let's activate the serial port and output those numbers:
<pre>/* LED Dimmer by Genius Arduino Programmer 2012 Controls the brightness of an LED on pin D9 based on the reading of a photocell on pin A0 This code is in the Public Domain */ // name analog pin 0 a constant name const int analogInPin = A0; // name digital pin 9 a constant name const int LEDPin = 9; //variable for reading a photocell int photocell; void setup() { Serial.begin(9600); } void loop() { //read the analog in pin and set the reading to the photocell variable photocell = analogRead(analogInPin); //print the photocell value into the serial monitor Serial.print("Photocell = " ); Serial.println(photocell); //control the LED pin using the value read by the photocell analogWrite(LEDPin, photocell); //pause the code for 1/10 second //1 second = 1000 delay(100); }
For more information about formulating code, visit the Foundations Page. If you need help with the Arduino Language, then the Language Page is the place for you.
Also, the Example Sketch Page is a great place to start messing around with code. Don't be afraid to change things and experiment.
GETTING STARTED WITH ARDUINO IDE
Reviewed by GAUTHAM RAJESH
on
11:28
Rating:
No comments: