6 Steps to get started with arduino programming language.

arduino uno | arduino programming language

Table of Contents

Step:-1:--arduino uno pins ,and intro to arduino boards

 

arduino types | arduino boards

To start with the arduino programming language, First you have to understand about the arduino board and its pin configuration. Trust me, it's very easy.

There are different types of arduino boards available in the market ,they are largely divided depending on the pin configuration.My focus is only on the arduino uno,and it is good for the learning purpose.

Some other arduino boards are:-

arduino uno pins:-

The arduino uno is the most used and the well documented board of the whole arduino family.

arduino uno pin diagram

 

Hardware specification

arduino uno consists of 14 digital pins and 6 analog input pins,now you are wondering what is analog and what is digital?

Digital means it has only two values either Yes or No,While analog means any value between a

a particular limit like 50 or 10 or 30 ,here limit is between 0 to 100.

Digital Pins can be used as input as well as output while analog pins cannot be used as analog input,but they can be used as digital output pins.

Here what I mean,Let's take an example of digital pin 13,You can configure this pin as digital input as well as digital output, it cannot be used as analog input or output

Now take an analog input pin,lets say A1 this pin can be used as an analog input and digital input.You can also take digital output from the A1 , but A1(analog pin number) cannot be used To give analog output.

There is one more pin type that is PWM output pins, they are alternatives of analog output. In arduino uno pin no 3,5,6,9,10,11 are PWM pins.They are also the digital pins so do not confuse yourself that it is some new type.

 

STEP-2:-Arduino IDE

arduino ide

Arduino ide is the Software used to write the arduino programs in the arduino programming language, it is the open source available both online as well as offline for Linux,Windows and Mac. You can download it from official website of arduino

 

 arduino board selection in Arduino ide

 

connect the arduino uno to the PC using USB cable and Select the arduino uno frrom the board type as shown above in the image.

 

Port checking in arduino ide

 

Now confirm the arduino uno is available to the port at tools>port>your_port

If you are using the genuine arduino uno board then you do not have any problem in connecting it to the Computer,But if you are using Chinese arduino uno board then you have to install the ‘Ch340’ arduino driver ,you can download it by clicking here

At last create a new file at FILE>NEW or press ‘ctrl+n’ and save it!.

Now you are ready to code. In arduino ide.

 

STEP:-3 Write our first arduino code 

Arduino sketch has two sections i.e void setup() and void loop().In void setup() we write the code which we want to execute one time only,while in void loop() section we write the code which will run in the loop ,I mean again and again until we turn off the arduino.

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
}

First we have to declare the pins of the arduino that we want to use , in this example I’m using pin 13 because it has in-build LED connected on this pin, so we don't require any external led to be plugged in, although you can connect the external LED between arduino pin 13 and GND.

now you have to blink the led continuously so this Code goes into the void loop() section.

To make the Led Lights up we want to make it HIGH ,and to make it turn off, we have to make it LOW

here what i mean.

void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Finally upload the code from the upload button at the top left side and you will see the LED blinking on your board.

Here is how complete code will look likes:-(You dont need to connect any external circuit for this just plug the usb to your PC and enjoy!)

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

 

STEP-4:-Working with digital pins

Now we will go further and build our own circuit to blink the led. We will use the arduino uno here.

We will require a breadboard, LED, arduino uno, and connecting wires for this experiment.We will start with writing the code for our arduino uno.

You already know we have two sections in arduino coding, void setup() and void loop(),We will write our void setup() code first.

In this section we have to set up our pins  as input or output.Let use digital pin 11 of arduino uno as an output.

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(11, OUTPUT);
}

 Now we will write the code for the void loop section, which is the same as our previous one.But we change the output pin 13 to 11.

void loop() {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

At last hit the upload button to upload code in the arduino uno and finally connect the circuit as shown below you will see the LED is blinking.

circuit for blinking led

Here is how complete code will look likes:-

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(11, OUTPUT);
}
void loop() {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

 

 

STEP-5:-Variables and Map function

Before writing the code I want to introduce you to two things, first one is variable and another one is map function.We will use these both in our code.

Let's start with the first one “ Variables ”. You can understand a variable as a bucket to put data in it which can be changed dynamically .What do you mean by dynamically?

Dynamically means the value which is not fixed. 

If you have trouble understanding it don't worry! Let take an example:-

To create a variable or bucket in arduino language we use the keyword ‘ int ’,variables created using the ‘ int ‘ keyword will take the number type data only, like 1,2 ,3,111,-22 etc.

int input_value=0
void setup()
{}
void loop()
{}

Here ‘ input_value ’ is the name of our variable (bucket),you can decide any name but there are certain rules for declaring variable names,and we write our variable outside the void setup() ,and  void loop() section , so we can use it anywhere in our code,such type of variables which can be used anywhere is called Global variables.

We have put 0 value in our variable .Here 0 is our data which we put in our bucket(variable) named “input_value”.

You can learn more about variables here, but that is not required in this tutorial.

Our second thing is the map function.Map function is simply used to Re- map the number from one range to another.It’s syntax looks like this.

syntax:-

map(value, fromLowerRange, fromHigherRange, toTargetRange(Low), toTargetRange(High));

Where:-

value: the number to map.
fromLowerRange:
the lower bound of the value’s current range.
fromHigherRange:
the upper bound of the value’s current range.
toTargetRange(Low):
the lower bound of the value’s target range.
toTargetRange(High):
the upper bound of the value’s target range.

I know it is quite confusing , but it is very easy trust me.Let take an example to understand the map function Suppose we have two ranges one from 1-10 and another is 1-100 and we have random number between 1-10 let say 2.Now what do you think if we want to map 2 in the range 1-100 ? Yes , you are right it will be 20 between the range 1-100,similarly for 5 it will be 50 and for 10 it will be 100.I think you get how we map numbers from one range to another .But how do we write this to the map function.let takes a look.

map(2,1,10,1,100);

 Map function will through the mapped value and we want a bucket(variable) to catch that value so our code looks like this.

int x=map(2,1,10,1,100);

Here ‘x’ is the name of our variable, which will take the value from the map function.

Let use these two things in our code.

Step-6 working with analog pins

Now we will use a potentiometer in the analog input pin  to control the brightness of led, and build our own circuit.. We will use the arduino uno here.

We will require a breadboard, LED, arduino uno,potentiometer, and connecting wires for this experiment.

If you don't know what a potentiometer is ? then here is a quick explanation for you, a potentiometer is an electronic component which is used to control the voltage in the circuit , it is similar to the regulator in your house fan.

potentiometer 

We will start with writing the code for our arduino uno.

You don't have to write the ‘void setup()’ section code from scratch ,we will use previous code and set up an analog pin ‘A0’ as input.

void setup()
{
  pinMode(A0, INPUT);
  pinMode(11, OUTPUT);
}

We wil also declare two variables , one for map value and another one for the input value, let see how we did it.

int input_value = 0;
int x = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(11, OUTPUT);


}

Now we will write the code for the void loop section.But before that setup a circuit as shown below .

circuit diagram

 

You don't need to know how a potentiometer works ,you just have to keep in mind that it will change the value of the voltage when we turn its nob.

Let's start the coding of the void loop() section,We will first take the value from the input pin which is our A0 pin , on this we are getting a value of voltage between 0 to 5v whose analog value is in the range of 0-1023 .

In short we are getting numbers from 0-1023 from our potentiometer on the pin A0.To read this value and to store it in the variable name “input_value”,we will write the following command.

void loop()
{

  input_value = analogRead(A0);

}

Now simply we have to write this value on the pin 11 where we connect our LED,but wait ! we have to write the value in between the range of 0-255 only,but we are getting value between 0-1023.So what do we do here? Here we will use the map function to map the value from the range of 0-1023 to the 0-255 , and store it into the variable name ‘x’.

void loop()
{
  input_value = analogRead(A0);
  x = map(input_value, 0, 1023, 0, 255);

}

Now we write the ‘x’ value on the pin 11.

void loop()
{

  input_value = analogRead(A0);
  x = map(input_value, 0, 1023, 0, 255);
  analogWrite(11, input_value);

}

Finally connect your arduino uno board and hit upload button and turn the knob of the potentiometer to watch the change in the brightness of the led.

Here is how complete code will look likes:-

int input_value = 0;

int x = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(11, OUTPUT);


}

void loop()
{
  x = 0;
  input_value = 0;
  input_value = analogRead(A0);
  x = map(input_value, 0, 1023, 0, 255);
  analogWrite(11, input_value);

}

Conclusion:-

arduino uno is the most used board in the arduino family and it is very simple to code.You have to follow these 6 steps and you are ready to code your first program in arduino programming language.

These steps are ‘Understanding arduino uno pins ,and intro to arduino boards’ it tells you about that arduino board has set of digital and analog pins used for different purpose and also has PWM output which will be used for special purpose,checkout the pin diagram above for better understanding. Next is ’Arduino ide setup’ ,this section will tell you about how you have to set the arduino ide software to upload your first code, next step is ‘writing your first code’, it will Guide to write your first code and upload it to the arduino uno.On moving further in step 4 , 5 , and 6 we will learn about variables and analog pins make dimmable led using potentiometer.There are also other boards available in the market like arduino nano, arduino mini ,but I focus only on the arduino uno board because it is the most famous one and best for the beginners.

If you have any problem message me from the contact section in the footer i will surely help you ,to solve your problem.

FAQ

Ans:- Probably you are using the chinese arduino board, Install the ch340 driver and you are ready to go.

Ans:- No, you don't need any special driver in Linux or mac OS but in Windows you need to install ch340 driver.

Ans:- Proof check your code with the mine given in the source code section above,Check for the capital, and small letters as arduino language is case sensitive language.If Error still exist contact me at contact@engineringproject.net or through the message box given in the footer.


Recent Post