How to Use the Areduino Uno to Upload to the Attiny5
Welcome to the starting time chapter of my Arduino Tutorial for Beginners. In this chapter, you volition learn the very nuts of the Arduino platform then that you can kickoff programming. I will teach y'all how to prepare your figurer and write your kickoff program for the Arduino Uno lath.
What better identify to get-go learning most the Arduino platform than with the Arduino Uno board. For PLC programmers it can often be an advantage to know microcontrollers. They are very like in many aspects, and knowing most microcontrollers tin can assistance your agreement of how the PLC works.
If you're in Europe the Arduino recently changed its name to Genuino.
The Arduino Uno or Genuino Uno board is a uncomplicated open-source electronics prototyping platform. It has an Atmel ATmega328P microcontroller which makes if perfect for learning about embedded programming and microcontrollers.
There are several reasons the Arduino Uno is perfect for beginners:
- Simple connection to a figurer
The Arduino Uno lath has a USB port and can therefore easily be connected to a figurer. All the drivers are even included in the Arduino IDE and so that'south the only affair you'll take to install. - Piece of cake wiring
All the pins on the ATmega328P microcontroller are continued to headers on the side. In that mode, you can wire it to a breadboard without whatever hassle. - Simplified programming language
When y'all are programming the Arduino Uno you will exist using a simplified programming language. Actually information technology is a simplified C programming language. - Lots of support
The whole Arduino platform is very popular and has millions of users. This ways that there's a lot of help to get when yous are having bug. You will exist able to Google many of your bug and detect many solutions.
Since it is and so good for beginners why don't nosotros just become ahead and start the Arduino Uno tutorial by setting upwards your computer? If y'all don't accept an Arduino Uno, you tin option up one here (affiliate link) or grab a good beginner'southward Arduino starter kit.
Arduino Uno Software
First of all, you want to install some software for the Arduino Uno.
There are several IDE's (integrated development editors) available to program the Arduino boards. In this tutorial, we will be using the Arduino IDE which is a gratis open source IDE past Arduino.
Arduino IDE is a very simple slice of software to program Arduino boards. The download and installation process is very simple.
Download the Arduino IDE hither.
When y'all accept downloaded the IDE y'all have to install information technology. Installing it is simple and you don't have to do much else than accepting terms of conditions and select the components you want to install.
Here are some instructions to install Arduino IDE on the different platforms:
Windows
Linux
MacOS
After the installation you can now first the Arduino IDE.
Well-nigh of the IDE will be a while field where your code goes, menus in the superlative and a last in the bottom.
Next step is to connect your Arduino Uno to your computer.
Connecting the Arduino Uno
Before you connect the Arduino Uno for the first time you should close the Arduino IDE.
To connect the board to your computer you will need a USB two.0 A/B cable.
On the side of the Arduino Uno in that location is a USB type B plug. Plug the cablevision into that and the other end to one of the USB ports on your computer. The board will automatically be powered upwardly via the USB and you volition see the ON LED light up green.
If yous're using Windows you will have to find the drivers the commencement fourth dimension you connect your Arduino. In the popular-up window y'all should cull to look up the drivers on your computer. When you lot click browse go to the binder where you lot've installed Arduino and choose the folder called Drivers.
Afterwards that yous tin kickoff the Arduino IDE.
Permit's check if the connexion to your Arduino Uno works.
Go to the menu Sketch -> Upload or printing Ctrl+U.
This should start uploading the code written in the white box to your Arduino. The code doesn't do annihilation or have any functions. It is just to check if yous take a connectedness to the board.
If the upload is successful you should see the bulletin "Done uploading." in a blue bar but above the black terminal window in the lesser of the IDE.
You might encounter the bar is orange instead of bluish with the message: "An fault occurred when uploading the sketch."
This is near likely considering you oasis't called the correct port.
Go to the carte du jour Tools -> Port and and so choose the COM port where (Arduino/Genuino Uno) is included in the proper noun.
Now try once again to upload the code (Sketch -> Upload or Ctrl+U) and information technology should work.
Congratulations! You are now ready to start programming with the Arduino Uno!
Your Start Arduino Uno Program
Earlier we get alee there'southward one new word you have to learn. In the Arduino IDE the code is not called a program but a sketch.
Since we are using the Arduino IDE I volition be using the discussion sketch for our lawmaking because it makes it easier to navigate in the IDE.
Let's start with our first sketch!
The only things you need for this is the Arduino Uno board and the USB cable. The Arduino Uno lath has a build-in LED (light emitting diode) connected to one of the digital input/outputs.
Arduino Uno Digital Pins
If you look at a pinout for the Arduino Uno board you will find that all the header connectors in the sides are connected to the microcontroller.
There are both pins for powering and communicating with the Arduino but what we need to look at now is the digital inputs/outputs.
At the top of the Arduino Uno lath, there are headers counting from 0 to 13 followed by one called GND. These are all digital inputs or outputs and the last one is the ground. Y'all can encounter on the Arduino board that they are all marked every bit "DIGITAL".
And so what are all these digital pins? And what exercise I hateful when I say they are both inputs and outputs?
Digital Inputs or Outputs
All the digital pins from 0 to 13 can be prepare to be either inputs or outputs. Yous will do that in the code.
We call these pins digital considering they have ii states.
HIGH and LOW.
These two states are the voltage level. For instance, if you lot ready one of the digital pins to exist an output (equally we're going to do soon) you tin prepare the output to be either Loftier or LOW in the code.
If the output is Depression the pin volition be at 0 volts. Just if it'south set to High the voltage level is five volts.
This is very useful for us. If you lot know a footling bit almost electronics yous will speedily realize that we can use this changing between 0 and v volts to control things.
Permit'due south go back to the code and see how we tin can set upward the digital pins.
Arduino Minimum Code
When you open the Arduino IDE you volition typically meet a slice of code that looks like this:
void setup() { // put your setup code hither, to run once: } void loop() { // put your main code here, to run repeatedly: }
This is really the minimum code your Arduino Uno board needs.
Setup and loop are both functions in C. C is the programming language used for Arduinos. In fact, it'south a simplified version of C, just the syntax is the same. Programming microcontrollers with C tin can quickly become complicated considering y'all have to deal with memory and so on. The fact that you don't accept to is 1 of the biggest advantages of the Arduino platform.
If you take never seen the C programming language before or want to improve your C programming skills, I highly recommend The C Programming Linguistic communication (affiliate link) by K&R. The first chapter of this C tutorial is also very helpful.
Commencement, accept a look at the setup office. Inside you will run across ane line of code. This is not really code only rather a comment. Everything on a line later on ii slashes is a annotate and volition be ignored by the Arduino.
Arduino Setup Function
As the comment says, this function will only run one time. It happens when you lot power up the Arduino or when you press the reset button.
This can come in very handy. Yous can use it as a function to setup variables and so on in your program. That'due south why the office is chosen setup. The Arduino will simply run this code one fourth dimension and then go to the primary part.
Remember that the digital pins on an Arduino can exist set to either inputs or outputs. The setup function is the perfect identify to ascertain that since you lot only need to do that one time.
Defining Output with pinMode()
In social club to define whether a digital pin on your Arduino Uno board should exist an input or an output, y'all take to use another function. This is too a standard Arduino part. Nosotros will be using the function pinMode().
Arduino uses simplified C with a lot of built-in functions. Equally described before, this will make it a lot easier to configure and program the board. The standard function for defining the digital pins is the pinMode() function.
If you await in the reference for the pinMode() function you will run across this:
pinMode(pivot, fashion)
This is the syntax of the office. You can run across that the function takes ii parameters: pin and mode.
pivot: The number of the pivot yous want to fix
style: To what you will define the pin (east.g. INPUT or OUTPUT)
With that in mind, let'south go dorsum to our first programme.
On the Arduino Uno board, in that location's a built-in LED. This LED is continued to pin # 13. Remember that this is an LED, and so nosotros want to prepare that pin to an output. This is how we will practice it:
void setup() { // put your setup code here, to run once: pinMode(13, OUTPUT); } void loop() { // put your main code hither, to run repeatedly: }
Yous can see that the pinMode() function is placed in the setup() function. This is because nosotros only need to set the pin ane fourth dimension.
Exist enlightened that the default state of the Arduino digital pins is input. So if you desire to apply them as inputs y'all don't even need the pinMode() function.
Built in LED and Constants
In that location's another way to gear up the pin number 13. The pin is special because information technology has the congenital in LED continued to it. So instead of but using the number thirteen in our function, we can use something called a abiding.
Like the functions Arduino also has some predefined constants.
A abiding is a predefined expression. Just accept a expect at the reference site for constants provided past Arduino. You will see some constants similar the pin levels (HIGH and Low), pin modes (INPUT, OUTPUT and INPUT_PULLUP). But if you coil downwards a fiddling further you will detect a constant called LED_BUILTIN.
This constant always refers to the pin where the built in LED is connected. On virtually Arduino boards information technology is connected to pin # 13. So instead of merely using 13 in the pinMode() part we could use LED_BUILTIN:
void setup() { // put your setup code here, to run in one case: pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your master code here, to run repeatedly: }
In this style our code becomes a little more generic. Y'all can at present use this code on any Arduino lath and not just the Arduino Uno.
Arduino Loop Role
Next pace is to look at the loop() role. Because now that nosotros've setup the pivot we need information technology's time to practise something with that pivot.
After running the setup() role the Arduino will go on to the loop() function and run that. But this function is a little unlike than the setup() function. The loop() function as the name says will run over and over. Like an space loop.
The loop is where the real fun happens. Information technology's here you will write the beliefs of the plan.
Since we already defined pin # 13 (LED_BUILTIN) to an output, let's start by turning on the output. This volition turn on the built in LED on the Arduino board. Because what nosotros volition be doing is setting the state of the output to Loftier. Equally said earlier the state of HIGH volition set the voltage level of the pin to 5V.
Setting Output with digitalWrite()
To alter the state of an output guess how?
Again, Arduino has a predefined office for doing and then. Just like the pinMode() office, this role too takes parameters. Here is how the syntax looks in the official reference from Arduino:
digitalWrite(pin, value)
The function also takes two parameters: pin and value.
pin: The number of the pin you want to write to
value: The value you want to give this pin
The values we're going to use are HIGH and LOW. These are the predefined constants that volition set the voltage level of the pin.
Since nosotros want to turn on the LED nosotros accept to employ HIGH. Because when we use 5 volts the LED will turn on. The function we accept to write in the loop volition look like this:
void setup() { // put your setup code here, to run in one case: pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(LED_BUILTIN, High); // turn the LED on (High is the voltage level) }
Take notation that I employ the constant LED_BUILTIN instead of simply number 13. Y'all can use this constant every fourth dimension you want to piece of work with this pin. It makes your code much easier to read and sympathise. You will ever run into that this is the pin where the built in LED is connected.
You can besides see that I've added a comment afterwards the line of lawmaking. This is also to make the code easier to read and sympathize. Peculiarly when you are learning it'southward very important to annotate your code.
It is now time to put this code into the Arduino and run it!
Compiling and Uploading the Sketch
I already showed you how to upload the sketch (program) to the Arduino board. Merely there is one thing yous always should practise before uploading. That is to verify your code. We have written some lines of lawmaking, and we desire to brand sure that they work. Because things can get wrong if you upload a piece of code with errors to the Arduino lath.
Verify the Arduino Lawmaking
With all the in a higher place code in the editor go to Sketch -> Verify/Compile or press Ctrl+R.
This will not upload the lawmaking to your Arduino. It will verify your code. Checking the code for whatsoever errors and make sure it volition work on your board.
If your code has no errors the verification will look like this:
Skilful chore! You've written Arduino code that works!
Simply what will happen if there is some errors in the Arduino code?
For the sake of learning, permit'south make a syntax error on purpose. After every expression in that location is a semicolon to cease the expression. In this way the compiler will know when an expression ends and a new one begins. Forgetting to put the semicolon is ane of the most common programming mistakes. Even for experienced programmers. Let'due south make that mistake and meet what happens.
Change line 4 from this:
pinMode(LED_BUILTIN, OUTPUT);
to this:
pinMode(LED_BUILTIN, OUTPUT)
Now, go to Sketch -> Verify/Compile or press Crtl+R
The verification will at present look a bit different:
What you volition see is that the status bar turns orange. At the same fourth dimension an fault message is shown in the status bar and ane line turns red. This is extremely helpful because you will both see what the error is and where it is.
The error message is:
expected ';' earlier '}' token
What this message ways is that the compiler needs to see a semicolon earlier the bracket.
Line 4 is an expression. But without the semicolon the expression never ends. The compiler will merely continue to read the code expecting the expression to go along or cease. When the compiler meets a subclass sign information technology gets dislocated. Because the subclass will end the setup function before the expression ends.
To set this, simply put the semicolon back on line 4.
Uploading Sketch to Arduino
Now that you have a lawmaking without errors information technology's fourth dimension to upload it to the Arduino lath.
Every bit described earlier this is done in the carte Sketch -> Upload or just by pressing Ctrl+U.
If you get the bulletin: "Done uploading." you have successfully compiled and uploaded your sketch to the Arduino board.
To those of you who have good eye you might have noticed something earlier the upload. Before uploading the Arduino IDE will compile your sketch. Compiling means that your code will exist translated into code that the Arduino (AVR microcontroller) can understand. A microcontroller is not equally intelligent every bit we are. In fact, the merely thing it tin can sympathise are 0's and 1'due south. All your code will be translated into 0's and ane'southward when compiling.
With the code now in the Arduino, take a wait at the board.
You should see that the LED on the board (shut to pin 13) called L1 is now on. This is because we've changed the country of the pin to Loftier. Pin 13 now has a voltage level of v volts.
Equally a little experiment, we will alter the code. Instead of setting the pin land to High nosotros will set it to Depression. This should gear up the voltage level to 0 volts (ground) and plough of the LED.
The code should await like this:
void setup() { // put your setup code here, to run in one case: pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(LED_BUILTIN, Low); // turn the LED off (Low is the voltage level) }
Look closely at the Arduino board while uploading this lawmaking.
You volition see that two other LED's starting time to glimmer. They are called "TX" and "RX". These 2 LED'due south are the indicators of communication. Every time they glimmer advice to the Arduino Uno lath is going on. Communication happens when you every bit here upload a sketch to the board.
Equally soon as the uploading process is done the LED's volition plough off. The built in LED volition besides turn off due to our new lawmaking.
Working with the Loop()
Let'southward dig a little deeper into this loop() office. Over again, this loop runs over and over again. We can use this to brand our programme a trivial more fun.
Right now we only have one line of code in the loop() function. This line of code will be executed over and over. Pin # 13 will be set to the same country over again and again. The issue volition be that the LED will always be either on or off.
What if we write two lines of code in the loop. 1 that turns the LED on and one that turns it off. This should in theory brand the LED blink.
Here is how out code will look now:
void setup() { // put your setup code hither, to run one time: pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your principal code here, to run repeatedly: digitalWrite(LED_BUILTIN, Loftier); // turn the LED on (High is the voltage level) digitalWrite(LED_BUILTIN, LOW); // plow the LED off (LOW is the voltage level) }
The loop volition now run these two lines of code over and over. First, turning the LED on and so off, and then on, and so off…
Try to upload the code and see what happens at the Arduino board. What happens at present is something that you might non have expected. The LED does non plough on and off, but rather shines vaguely.
This is because the Arduino is fast. Very fast. Faster than the homo center. As a matter of fact the LED actually blinks. It blinks then fast that you will neither see the LED on or off. You will see the avarage of this. Since the LED is on 50% of the time and off 50% of the time yous will see the LED shining with l% intensity.
Ho-hum Downward the Lawmaking with Delay()
What we want to exercise now is to slow downward the lawmaking a bit. Slow it down so we can meet the LED glimmer.
This is done by putting in delays. Then that later on the LED is turned on nosotros desire to wait a scrap. The same afterwards nosotros have turned it off. In that way we will have time plenty to see the LED blink.
Here again we are lucky to use the Arduino platform. Considering the Arduino has a predefined function for putting in delays in our programme. This role is simply called delay() and takes ane parameter. This is how the syntax looks like in the Arduino reference:
delay(ms)
The parameter in this role is ms. Here goes the corporeality of time you lot want the delay to exist. The delay is set up in milliseconds. In i second there are yard milliseconds (ms).
Permit's put these delays into our code. One-half a second should be enough to see the LED glimmer. Nosotros will therefore put 500ms every bit a parameter in the filibuster functions:
void setup() { // put your setup lawmaking here, to run in one case: pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your primary code here, to run repeatedly: digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(500); // Await 500 milliseconds digitalWrite(LED_BUILTIN, LOW); // turn the LED off (Depression is the voltage level) delay(500); }
Accept notation that I put the delay() function both afterward turning the LED on and off. By doing so the LED volition be on for 500ms and then off for 500ms.
Try to upload this lawmaking and see how the LED now behaves.
You at present got a blinking LED!
And the LED keeps blinking since the loop runs over and over again.
Source: https://www.plcacademy.com/arduino-tutorial-for-beginners-chapter-1/
Enregistrer un commentaire for "How to Use the Areduino Uno to Upload to the Attiny5"