Program At90s2313 With Arduino Tutorials

An interrupt, in microcontroller context, is a signal that temporarily stops what the CPU is currently working at. Programming using interrupts is very different from the usual top-to-bottom sequence in an Arduino program and thus can be confusing for some. This article aims to introduce how an interrupt works and how you can use it.

Arduino Code & Syntax Overview

As you learned in Module 01, IDE stands for Integrated Development Environment. Pretty fancy sounding, and should make you feel smart any time you use it. The IDE is a text editor-like program that allows you to write Arduino code.

When you open the Arduino program, you are opening the IDE. It is intentionally streamlined to keep things as simple and straightforward as possible. When you save a file in Arduino, the file is called a sketch – a sketch is where you save the computer code you have written.

The coding language that Arduino uses is very much like C++ (“see plus plus”), which is a common language in the world of computing. The code you learn to write for Arduino will be very similar to the code you write in any other computer language – all the basic concepts remain the same – it is just a matter of learning a new dialect should you pursue other programming languages.


If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.

The code you write is “human readable”, that is, it will make sense to you (sometimes), and will be organized for a human to follow. Part of the job of the IDE is to take the human readable code and translate it into machine-readable code to be executed by the Arduino. This process is called compiling.

The process of compiling is seamless to the user. All you have to do is press a button. If you have errors in your computer code, the compiler will display an error message at the bottom of the IDE and highlight the line of code that seems to be the issue. The error message is meant to help you identify what you might have done wrong – sometimes the message is very explicit, like saying, “Hey – you forget a semicolon”, sometimes the error message is vague.

Why be concerned with a semicolon you ask? A semicolon is part of the Arduino language syntax, the rules that govern how the code is written. It is like grammar in writing. Say for example we didn’t use periods when we wrote – everyone would have a heck of a time trying to figure out when sentences started and ended. Or if we didn’t employ the comma, how would we convey a dramatic pause to the reader?

And let me tell you, if you ever had an English teacher with an overactive red pen, the compiler is ten times worse. In fact – your programs WILL NOT compile without perfect syntax. This might drive you crazy at first because it is very natural to forget syntax. As you gain experience programming you will learn to be assiduous about coding grammar.

Let’s get our hands dirty and introduce some syntax.

The Semicolon ;

A semicolon needs to follow every statement written in the Arduino programming language. For example…

In this statement, I am assigning a value to an integer variable (we will cover this later), notice the semicolon at the end. This tells the compiler that you have finished a chunk of code and are moving on to the next piece. A semicolon is to Arduino code, as a period is to a sentence. It signifies a complete statement.

The Double Backslash for Single Line Comments //

Comments are what you use to annotate code. Good code is commented well. Comments are meant to inform you and anyone else who might stumble across your code, what the heck you were thinking when you wrote it. A good comment would be something like this…

Now, in 3 months when I review this program, I know where to stick my LED.

Comments will be ignored by the compiler – so you can write whatever you like in them. If you have a lot you need to explain, you can use a multi-line comment, shown below…

Comments are like the footnotes of code, except far more prevalent and not at the bottom of the page.

Curly Braces { }

Curly braces are used to enclose further instructions carried out by a function (we discuss functions next). There is always an opening curly bracket and a closing curly bracket. If you forget to close a curly bracket, the compiler will not like it and throw an error code.

Remember – no curly brace may go unclosed!

Functions ( )

Let’s switch gears a bit and talk about functions.

Functions are pieces of code that are used so often that they are encapsulated in certain keywords so that you can use them more easily. For example, a function could be the following set of instructions…

This set of simple instructions could be encapsulated in a function that we call WashDog. Every time we want to carry out all those instructions we just type WashDog and voila – all the instructions are carried out.

Paas

In Arduino, there are certain functions that are used so often they have been built into the IDE. When you type them, the name of the function will appear orange. The function pinMode(), for example, is a common function used to designate the mode of an Arduino pin.

What’s the deal with the parentheses following the function pinMode? Many functions require arguments to work. An argument is information the function uses when it runs.

For our WashDog function, the arguments might be dog name and soap type, or temperature and size of the bucket.

The argument 13 refers to pin 13, and OUTPUT is the mode in which you want the pin to operate. When you enter these arguments the terminology is called passing. You pass the necessary information to the functions. Not all functions require arguments, but opening and closing parentheses will stay regardless though empty.

Notice that the word OUTPUT is blue. There are certain keywords in Arduino that are used frequently and the color blue helps identify them. The IDE turns them blue automatically.

Now we won’t get into it here, but you can easily make your own functions in Arduino, and you can even get the IDE to color them for you.

We will, however, talk about the two functions used in nearly EVERY Arduino program.

void setup ( )

The function, setup(), as the name implies, is used to set up the Arduino board. The Arduino executes all the code that is contained between the curly braces of setup() only once. Typical things that happen in setup() are setting the modes of pins, starting

You might be wondering what void means before the function setup(). Void means that the function does not return information.

Some functions do return values – our DogWash function might return the number of buckets it required to clean the dog. The function analogRead() returns an integer value between 0-1023. If this seems a bit odd now, don’t worry as we will cover every common Arduino function in depth as we continue the course.

Let us review a couple things you should know about setup()…

1. setup() only runs once.

2. setup() needs to be the first function in your Arduino sketch.

3. setup() must have opening and closing curly braces.

void loop( )

You have to love the Arduino developers because the function names are so telling. As the name implies, all the code between the curly braces in loop() is repeated over and over again – in a loop. The loop() function is where the body of your program will reside.

As with setup(), the function loop() does not return any values, therefore the word void precedes it.

Does it seem odd to you that the code runs in one big loop? This apparent lack of variation is an illusion. Most of your code will have specific conditions laying in wait which will trigger new actions.

If you have a temperature sensor connected to your Arduino for example, then when the temperature gets to a predefined threshold you might have a fan kick on. The looping code is constantly checking the temperature waiting to trigger the fan. So even though the code loops over and over, not every piece of the code will be executed every iteration of the loop.

Try On Your Own

This course is based around the example sketches provided with the Arduino IDE. Open up your Arduino IDE and go to File > Example > 01.Basics and open up three different sketches. Identify the following syntax and functions that you find in the sketches:

  • ; semi-colons
  • // single line comments
  • /* */ multi-line comments
  • { } open and closing curly braces
  • ( ) parenthesis
  • void setup() – identify the opening and closing curly braces
  • void loop() – identify the opening and closing curly braces
  • some blue keywords like OUTPUT or INPUT

Further Reading

Go to the Arduino Reference page and check out all the syntax under Further Syntax that we talked about. You will find some further useful info on each of these elements.

  • Arduino Tutorial
  • Arduino Function Libraries
  • Arduino Advanced
  • Arduino Projects
  • Arduino Sensors
  • Motor Control
  • Arduino And Sound
  • Arduino Useful Resources
  • Selected Reading

In this chapter, we will interface different types of motors with the Arduino board (UNO) and show you how to connect the motor and drive it from your board.

There are three different type of motors −

  • DC motor
  • Servo motor
  • Stepper motor

A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.

Warning − Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC.

We will divide this chapter into three parts −

  • Just make your motor spin
  • Control motor speed
  • Control the direction of the spin of DC motor

Components Required

You will need the following components −

  • 1x Arduino UNO board
  • 1x PN2222 Transistor
  • 1x Small 6V DC Motor
  • 1x 1N4001 diode
  • 1x 270 立 Resistor

Procedure

Program At90s2313 With Arduino Tutorials

Follow the circuit diagram and make the connections as shown in the image given below.

Precautions

Take the following precautions while making the connections.

  • First, make sure that the transistor is connected in the right way. The flat side of the transistor should face the Arduino board as shown in the arrangement.

  • Second, the striped end of the diode should be towards the +5V power line according to the arrangement shown in the image.

Spin ControlArduino Code

Code to Note

The transistor acts like a switch, controlling the power to the motor. Arduino pin 3 is used to turn the transistor on and off and is given the name 'motorPin' in the sketch.

Result

Motor will spin in full speed when the Arduino pin number 3 goes high.

Motor Speed Control

Following is the schematic diagram of a DC motor, connected to the Arduino board.

Arduino Code

Code to Note

The transistor acts like a switch, controlling the power of the motor. Arduino pin 3 is used to turn the transistor on and off and is given the name 'motorPin' in the sketch.

When the program starts, it prompts you to give the values to control the speed of the motor. You need to enter a value between 0 and 255 in the Serial Monitor.

In the 'loop' function, the command 'Serial.parseInt' is used to read the number entered as text in the Serial Monitor and convert it into an 'int'. You can type any number here. The 'if' statement in the next line simply does an analog write with this number, if the number is between 0 and 255.

Result

The DC motor will spin with different speeds according to the value (0 to 250) received via the serial port.

Spin Direction Control

To control the direction of the spin of DC motor, without interchanging the leads, you can use a circuit called an H-Bridge. An H-bridge is an electronic circuit that can drive the motor in both directions. H-bridges are used in many different applications. One of the most common application is to control motors in robots. It is called an H-bridge because it uses four transistors connected in such a way that the schematic diagram looks like an 'H.'

We will be using the L298 H-Bridge IC here. The L298 can control the speed and direction of DC motors and stepper motors, and can control two motors simultaneously. Its current rating is 2A for each motor. At these currents, however, you will need to use heat sinks.

Components Required

You will need the following components −

  • 1 × L298 bridge IC
  • 1 × DC motor
  • 1 × Arduino UNO
  • 1 × breadboard
  • 10 × jumper wires

Procedure

Following is the schematic diagram of the DC motor interface to Arduino Uno board.

The above diagram shows how to connect the L298 IC to control two motors. There are three input pins for each motor, Input1 (IN1), Input2 (IN2), and Enable1 (EN1) for Motor1 and Input3, Input4, and Enable2 for Motor2.

Since we will be controlling only one motor in this example, we will connect the Arduino to IN1 (pin 5), IN2 (pin 7), and Enable1 (pin 6) of the L298 IC. Pins 5 and 7 are digital, i.e. ON or OFF inputs, while pin 6 needs a pulse-width modulated (PWM) signal to control the motor speed.

The following table shows which direction the motor will turn based on the digital values of IN1 and IN2.

IN1IN2Motor Behavior
BRAKE
1FORWARD
1BACKWARD
11BRAKE

Pin IN1 of the IC L298 is connected to pin 8 of Arduino while IN2 is connected to pin 9. These two digital pins of Arduino control the direction of the motor. The EN A pin of IC is connected to the PWM pin 2 of Arduino. This will control the speed of the motor.

To set the values of Arduino pins 8 and 9, we have used the digitalWrite() function, and to set the value of pin 2, we have to use the analogWrite() function.

Connection Steps

  • Connect 5V and the ground of the IC to 5V and the ground of Arduino, respectively.
  • Connect the motor to pins 2 and 3 of the IC.
  • Connect IN1 of the IC to pin 8 of Arduino.
  • Connect IN2 of the IC to pin 9 of Arduino.
  • Connect EN1 of IC to pin 2 of Arduino.
  • Connect SENS A pin of IC to the ground.
  • Connect Arduino using Arduino USB cable and upload the program to Arduino using Arduino IDE software.
  • Provide power to Arduino board using power supply, battery, or USB cable.

Arduino Code

Result

Program

The motor will run first in the clockwise (CW) direction for 3 seconds and then counter-clockwise (CCW) for 3 seconds.