Hi. Now that I know how to program an Arduino uno at its basics, I would like to start to experiment a little without having it plugged into my computer. How do I add an external source of power to power on the Arduino. Also, will it run the most recent upload program? Or do I have to set that up? THX!
Welcome to the forum.
Being relatively new to Arduino, you should stick to using a wallwart 7-12VDC which is plugged into the power jack.
Your last loaded sketch will run.
Thank you. I looked into that and saw that it had to be plugged into a power outlet. That would work and I appreciate it but what is the easiest way to use something like a 9V battery that can be powered by attaching a cap.
EQOxx123:
Hi. Now that I know how to program an Arduino uno at its basics, I would like to start to experiment a little without having it plugged into my computer. How do I add an external source of power to power on the Arduino. Also, will it run the most recent upload program? Or do I have to set that up? THX!
It will automatically run the latest program that you uploaded. After all, it doesn't have another program to run. You replace the old program each time you upload a new one to the board.
You didn't mention what voltage the external supply is.
If it's a 5V regulated supply, you can connect directly to the 5V connection on the board.
If it's 7VDC to 12VDC, you can either connect to the "Vin" connection on the board, or using the barrel jack on the end of the board, (handy for plugpacks).
It's best to use 7VDC to 9VDC for Vin or the barrel jack if possible. That avoids heating the on-board voltage regulator while dissipating excess power.
Any source of 7VDC to 9VDC will do, as long as it provides enough current for whatever you plan to connect to the Arduino.
And don't use the Arduino to directly power any accessories that use much current, especially if the input voltage is towards the upper end of the 7V to 12V range.
The Arduino regulator can only handle a maximum of about 800mA and it should be kept well below that.
And you don't want the Arduino regulator dissipating more than a couple of Watts. This can be calculated by:-
(Vin-5V) * I, where I is the current that you're drawing through the regulator.
ie With a 9V supply connected to the barrel jack, and accessories totalling 0.5A current draw, the regulator's power dissipation will be a little over (9-5)*0.5 W, (2W).
N.B. @LarryD, I noticed that you posted while I was typing, but after typing all of this I decided to go ahead and post it.
@EQOxx123, what would the cap that you mentioned be for?
Thank you but what do you mean 7V to 9V. The cap to a battery that transfers the electricity when attached. The cap is attached to conductors(wires). Thx, Ethan.
EQOxx123:
Thank you but what do you mean 7V to 9V. The cap to a battery that transfers the electricity when attached. The cap is attached to conductors(wires). Thx, Ethan.
I thought 7V to 9V was fairly clear: a voltage somewhere between 7VDC and 9VDC.
Right, that cap, usually referred to as a battery connector.
A cap usually refers to a capacitor in this world.
Ohhhhh. Don't know what I was thinking about the 7V to 9V. Re-reading it makes complete sense... still don't follow with the capacitor thing but whatever . How do you know how much volts you are "using?" Don't know the proper term. Also, since my posting is limited I am just going to post something I just made quickly for the stream to see on this for I do not understand the error, but i think it has to do with the timing,(milli).
const int pin12 = 12;
const int LED = 13;
int ledstate = 13;
int val = 0;
int main();
void setup()
{
pinMode(pin12,OUTPUT);
pinMode(LED,OUTPUT);
}
void loop()
{
if (LED == LOW)
{digitalWrite(pin12,HIGH);}
else if (LED == HIGH)
{digitalWrite(pin12,LOW(2000));
digitalWrite(pin12,HIGH(2000));}
else if (ledstate > val)
{digitalWrite(LED,LOW);}
else if (ledstate <= val)
{digitalWrite(LED,HIGH);
}
Arduino: 1.6.5 (Mac OS X), Board: "Arduino/Genuino Uno"
sketch_nov02a.ino: In function 'void loop()':
sketch_nov02a:16: error: expression cannot be used as a function
sketch_nov02a:17: error: expression cannot be used as a function
sketch_nov02a:22: error: expected '}' at end of input
expression cannot be used as a function
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Sorry. By the time I finished this I solved the problem. It was the time but how do I incorporate time into simple outputs like the onboard LED.
EQOxx123:
Ohhhhh. Don't know what I was thinking about the 7V to 9V. Re-reading it makes complete sense...
Good.
still don't follow with the capacitor thing but whatever
"cap" = capacitor. The thing that connects a battery is a "battery connector"
How do you know how much volts you are "using?" Don't know the proper term.
The voltage that you're "using" is the voltage that the battery or plugpack supplies. ie If you have a 9V battery, you're "using" 9 volts.
I think you mean current usage. More often then not, the datasheet for the device (accessory) that you're connecting will state this. Sometimes you need to calculate it yourself. And sometimes, you yourself decide how much current will be drawn. It depends on the device. For instance, in the case of a LED, you determine the current yourself. If the LED has a forward voltage of 2.0V, and you're connecting it to a 5V supply, you connect a resistor in series with the LED. The resistor will have 3V, (5V-2V), across it. If you decide to run the LED at 10mA current, the calculation is R=V/I, which is 3/10x10-3, and the result is 300. The nearest standard resistor value is 330Ω, so you'd use that. Then the actual current through the LED would be I=V/R = 3/330 = 9.1mA, (9.1x10-3)
You may not understand all of this yet, but you will need to. Do a bit of reading on basic electronics. It's necessary if you plan to use an Arduino effectively.
Also, since my posting is limited I am just going to post something I just made quickly for the stream to see on this for I do not understand the error, but i think it has to do with the timing,(milli).
It is bad practice to post your code and errors inline in a post like this. You need to place it within code tags. You'll find the code tags button </> at the top left of the "Post" window. It's not too late to edit your post and correct this.
Sorry. By the time I finished this I solved the problem. It was the time but how do I incorporate time into simple outputs like the onboard LED.
For now, you could look at the "delay()" function, but really you need to learn to use the "millis()" function effectively. For a 1 second delay:-
unsigned long prevMillis=millis();
while(millis()-prevMillis<1000)
{
// Do stuff here
}
Or you can do a similar thing with an "if()" statement. Some reading is necessary to learn a bit more about the Arduino and the C++ language. There are plenty of tutorials online.
Simplest way - get a 9v power supply with a barrel connector, plug it into your arduino. They sell 'em everywhere.
Next-most-simple way, get a 9v battery or a battery pack that takes 4 1.5v cell batteries (eg, AA batteries, C-cell, whatever). Black wire goes to earth, red wire goes to Vin.
Neater way - attach a barrel connector to your batter pack. Black wire goes to the outside of the barrel (the sleeve), red wire goes to the inside of the barrel. Plug connector into arduino.
DC electrical power has two (main) measurements - volts and amps. An amp is how many electrons flow past a point in a given amount of time. A volt is (more or less) the force behind the flow. Power, measured in watts, is volts times amps: how many electrons you get times the force with which you get them.
A battery only has a certain amount of electrons in the chemicals in it, so they are rated in "milliamp-hours": one milliamp-hour is enough juice for one milliamp to be drawn for one hour while still maintaining the battery voltage at a decent level. After that, the battery has run out of puff. Oh - it still has electrons, but they won't arrive at the force that it says on the label.
Electrical devices draw X amount of current. Divide the rating of the battery (in milliamp-hours) by the current draw of the component (in milliamps), and you get the working life of the battery (in hours).
More or less.
A better answer to your question, however, might be: "Meh - it doesn't draw that much. Just leave the power adapter plugged into the socket."
-- EDIT --
Oh BTW, one Columb - one Amp-Second - is about 16,021,766,208,980,000,000 electrons. In case you were wondering. That many electrons held as a static charge on an object would be a bad idea.
while(millis()-prevMillis<1000)
{
// Do stuff here
}
This is blocking.
if(millis()-prevMillis<1000)
{
// Do stuff here
}
This is non blocking.
LarryD:
while(millis()-prevMillis<1000)
{
// Do stuff here
}
This is blocking.
True, but it can be OK depending on what else is being done. If 'everything' is inside that while loop it's fine.
(But that's why I also referred to the 'if()' method.)
I didn't put much thought or info into the mention of millis()-based timing because I figured he'd go for delay() for now anyway. Everyone does when starting.
To fully explain millis()-based timing would have meant also explaining why 'unsigned long' is so important, and explaining the use of 'UL' with large unsigned long 'literal constants'.
All too much for him right now, I reckon.
Just pointing out "blocking" to the OP not you.
I hate blocking code.
LarryD:
I hate blocking code.
Yep, me too. I normally wouldn't even mention using 'delay()' either, but it's nice and simple when first starting, and it doesn't take long to realise that something more eloquent is needed.
And @EQOxx123, 'blocking' code is code that prevents other operations from continuing while it's running. 'delay()' is one of the best examples of this - program execution halts until the end of the delay period.
It's OK for basic timing for LEDs etc when you're starting out, but no good when you start to do more advanced things.
BTW
if(flag && millis() - prevMillis < 1000)
{
// Do stuff here
}
else
{
flag = false;
}
May be better, if prevMillis is "only set once" and the sketch runs for months.
millis() will over flow in 49 days thus this test can come true again and again if flag is not used.
Thank you everyone; it all makes a lot more sense now but what do you mean "blocking" the code. Thx/
EQOxx123:
Thank you everyone; it all makes a lot more sense now but what do you mean "blocking" the code. Thx/
I tried to explain that earlier, when I wrote this:-
'blocking' code is code that prevents other operations from continuing while it's running. 'delay()' is one of the best examples of this - program execution halts until the end of the delay period.
It's OK for basic timing for LEDs etc when you're starting out, but no good when you start to do more advanced things.
Obviously I didn't explain it well enough.
With a 'blocking' delay, no other statements are executed until the delay finishes. This can be bad if, for example, you have a long delay and a button is pressed during that time. The button press will not be registered.
With a 'non-blocking' delay, (like the millis()-based 'if()' example), other statements continue being executed while the delay times out. So the button press from the last example will be registered and acted upon.
In the Arduino IDE, under >File >Examples >Digital, there's a sketch called "BlinkWithoutDelay" that will help you to understand the concept.
EQOxx123:
Thank you everyone; it all makes a lot more sense now but what do you mean "blocking" the code. Thx/
The word "blocking" comes from the world of real multtasking. You can have 16 processors, but only one at a time can (say) talk to the disk drive. So if two things need to talk to the disk drive at once, then one of them will "block" the other - the process scheduler will force it to go idle until the disk array is free again.
The arduino only has one thread, of course, so everything that happens on it is neither blocking nor nonblocking. However, it's possible to program in a way that lets several things happen at once by giving slices of time to each. This makes sense on an arduino, because the chip runs faster than anything that happens in the real world. Mostly.
But if you need to lift a model train drawbridge and lower it 12s later, AND ALSO flash a "don't walk" sign, you can't lift the bridge for 12s using delay(). During the delay, you can't give time slices to other tasks.
PaulMurrayCbr:
The word "blocking" comes from the world of real multtasking. You can have 16 processors, but only one at a time can (say) talk to the disk drive. So if two things need to talk to the disk drive at once, then one of them will "block" the other - the process scheduler will force it to go idle until the disk array is free again.The arduino only has one thread, of course, so everything that happens on it is neither blocking nor nonblocking. However, it's possible to program in a way that lets several things happen at once by giving slices of time to each. This makes sense on an arduino, because the chip runs faster than anything that happens in the real world. Mostly.
But if you need to lift a model train drawbridge and lower it 12s later, AND ALSO flash a "don't walk" sign, you can't lift the bridge for 12s using delay(). During the delay, you can't give time slices to other tasks.
The term 'blocking' may originate from 'real multi-tasking', but it has become a standard term among Arduino users, with a definition as described above, because the delay 'blocks' execution of other statements. The definition may have changed slightly, but the term is valid in the world of Arduino.
Ohhhh. I have looked at that example and now I know what you mean. Thx both of you! I didn't realize that you meant that really only one command at a time in simple words.