Arduino Uno to ATmega328 - Shrinking your Arduino Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll be working with the heart of the Arduino the 80 mega 328 chip we'll learn how to use an 18 mega [Music] hello and welcome to the workshop now here in the drone bot workshop you've seen me build a number of projects with an Arduino and most of the time I have used the Arduino Uno the Uno is a very inexpensive Arduino especially when you get one of the clones and it's probably the most popular Arduino and this is one of the reasons I've chosen to use it for most of my projects however there comes a time when you're finished with your project and you may like to make a permanent version of it and when you do you have a number of choices one of your choices of course is just to use the unil itself and build it into an enclosure along with the rest of the components or shields that you need to complete your project and that's an excellent choice but naturally that ends up using your Arduino Uno and if you want to do some experimenting you'll need to get another Arduino Uno your second choice is to go and use one of the smaller models of Arduino now there's the Arduino Nano and the Arduino Pro Mini the Nano is a great little device that pretty well emulates everything in uno does it even has two extra analog pins on it and it has a USB connector etc it can be used in place of an Arduino Uno just fine and as it's much smaller it'll probably fit into a smaller enclosure the Arduino Pro Mini is even tinier but it lacks the USB port but your project may not even need a USB port so those are both very good choices but today I'm going to talk about a third choice and the third choice is to use the actual tip the actual microcontroller unit that is on the Arduino Uno and that tip is called an 18 mega 328 now the 18 mega 328 is available in both a surface mount package and in a 28 pin dip or dual inline package and it's a 28 pin dip that we're going to be working with today you can take your Arduino project and take everything and put it on to an 18 mega 328 and then all you need to do is add a few components to it basically a crystal a couple of and a resistor and you've got a complete working circuit that emulates everything you can do with your Arduino Uno assuming that you don't need a serial port and if you do need a serial port there's a way around that as well so today what I'm going to show you is how the 18 mega 3:28 works and how we can take on our dueño project and move it down to the 18 mega 3:28 and what I've done for this is I've got a very simple but rather cute Arduino project that you can build and then I'll show you how we can set it down onto a chip what you might want to do if you're making a printed circuit board if you're mass producing your project or if you just want to make a very tiny permanent version of your project so let's go ahead and learn a little bit about the 80 mega 3:28 so let's take a closer look at the 18 mega 3:28 the 18 mega 3:28 is a single chip microcontroller it was originally developed by axe Mel Atmel was bought by microchip technology who now owned the patent to the 18 mega 3:28 it's an 8-bit RISC processor risk is a reduced instruction set computer it has 32 kilobytes of flash memory it also has 2 kilobytes of static RAM and one kilobyte of electrically erasable programmable read-only memory or a EEPROM the 18 mega 3:28 is available both in dual inline package and surface mount packages the processor has 23 general purpose input output lines it has 32 general-purpose registers the 18 mega 3:28 also has 3 timer counters it has both I 2c and SPI interfaces it has either 6 or 8 10 bit analog to digital converters the dip package only has 6 of them whereas the surface mount package has 8 of them this explains why devices like the Arduino Pro Mini and the Arduino Nano actually have two extra analog to digital converters compared to an Arduino Uno it has both internal and external interrupts now we'll be using the dip or dual inline package version of the 18 mega 328 here are the functions of the 28 pins on this package you will note that most of the pins have multiple functions and here's the same pinout described in relation to the pins on an Arduino Uno so now let's start using the 18 mega 3:28 so now that we've learned a little bit about the 80 mega 328 I want to show you the Arduino project that I'm going to build and then transfer to run on an 80 mega 328 now the project is very simple in addition to the Arduino Uno it requires a couple of resistors a couple of LEDs and a piezo electric buzzer sound er now you may not have a piezo electric buzzer sound er but they're very commonly available you can get them on eBay or Amazon you can also probably pick one up at your local electronics store assuming you have a local electronics store and that's actually where I got mine now what our project is going to do is it's going to play a simple little tune on the PSO buzzer and it's going to flash the LEDs in time with the tune now if you're a star Wars fan you'll probably recognize the tune and you'll probably see that this would be a good circuit to build into an old toy if you have a Darth Vader or stormtrooper toy this would actually be something excellent to build inside it and when you do that of course you're not going to want to use a whole Arduino Uno something like an 80 mega 3:28 is ideal for this so I'm going to show you the code and then I'm going to show you our little toy in action and then after that we'll see how we can move the whole thing over to an 80 mega 3:28 so here's how I hooked up our music box project using an Arduino Uno in addition to the Arduino Uno I have two LEDs I chose a red and a green one but you can use any colors you like I also have two dropping resistors for the LEDs I use 220 ohms but any value from 150 to 330 ohms would probably work just fine I also have a piezo electric buzzer this is a very inexpensive component that you can probably pick up online if not at your local electronics shop I started by wiring the positive terminal of piezo buzzer to pin eight of the Arduino Uno I then connected the other side of the buzzer to the Arduino Uno is ground next I wired my red LED through the dropping resistor to pin 13 of the Arduino Uno the green LED goes through its dropping resistor and is connected to pin 12 of the Arduino finally I connected the cathodes of both of the LEDs to the ground and that completes the wiring so let's look at the sketch that we'll be using for our Arduino musical box now this sketch is not my own creation this was downloaded from github and it was originally written six years ago by a gentleman named Nick sort and many people have enjoyed his fine work and I am just one of them so with that said let's go through how this sketch works now at the beginning we define a number of constants and these constants represent the musical notes and the frequency of that specific musical note now if you know something about music you might recognize a few of these for example this one a at 440 Hertz is also referred to as a above middle C because of its position on a grand piano keyboard and this is a very common frequency for tuning forks and so you can see the frequencies we've defined for C and D and E and F and G a G sharp and a and a sharp and a B now these ones with an H path rhythm are an octave above the ones before because octaves work by a doubling of frequency so for example the a above middle C we have here is at 440 Hertz if we go to the bottom we have an A in the next octave which is twice the frequency of 880 Hertz now if you want to expand this and play a different tune you can't expand the range over here this totally does a small range of all the octaves that would be covered by a piano keyboard but keep in mind that the tones at the high end and the low end of the keyboard are not going to be reproduced properly if at all on the piezo buzzer that we're using and so after defining the musical notes we just define a few other constants the buzzer pin which is pin 8 where we've connected our piezo electric buzzer led one on pin 12 and led two on pin 13 we also define an integer for a counter which we will use to step through the music now in the setup it's very simple we simply set all of the pins out as outputs for the buzzer and the two LEDs now before we go in the loop I want to go and show you because it's the key of making this work and the function is called beep and beep takes two inputs two integers the note which is one of the notes we defined earlier and the duration in milliseconds that we will be playing the note and it uses the tongue function now the tone library is one of the built-in libraries in your arduino ide which is why it doesn't need to be declared in this program and so basically we use tone to go to the buzzer pin the piezo electric buzzer and we play the note the frequency of the note that has been sent to us for the duration and milliseconds and here we dance the LEDs and here's where the counter goes now we take a look to see if the counter is even or odd that's what this first statement does is see if it's a multiple of two or if it equals to zero because we're including zero as being an even number in this case now if it is an even number then we go to LED number one and we turn it on and then we keep it on for the length of the duration that we've been past at the top here and then we turn it off if not we go and we use LED two and we use the same thing we turn it on for the length of the duration and then we turn it off then after that we stop the tone and a buzzer and then we delay for 50 milliseconds and then we increment the counter by one so the result of this is every time we call beep it will play the tone for the duration that we wanted and it'll flash one of the two LEDs on for the same duration and then delay itself for 50 milliseconds and so now that you've seen beep we can go and take a look at these other two functions or music is defined by a couple of different sections because it repeats over and over again so Nick is dis simply chosen to make these into functions and this pattern goes on and on and on basically you call beep with the tone that you want to play the note here you're playing an A for 500 milliseconds another a for 500 milliseconds remember there's going to be a 50 millisecond delay after each one of these and then after this section you delay for half is second and place some other notes delay for half a second the second section works in a similar fashion just with different notes and if we go back up to the loop we'll find that's pretty well how the loop works in the loop we play the first section then we play the second section and then we do what he calls a variant which is just a bunch of different notes don't belong in either section delay we repeat the second section do another variant we delay again for a different period and play the loop over and over again so the loop represents one entire play of the tune which this repeats itself over and over so now that we've seen how the musical sketch works let's load it up to Arduino and take a listen to our Star Wars musical sketch so I've loaded my musical sketch up to the Arduino I've disconnected the USB cable for the moment so it doesn't start playing right away and as you can see I've got my piezo buzzer which is wired one side to ground the other side to pin eight I've got my green LED with a resistor a dropping resistor here and that's wired up to pin 12 and I've got my red LED with its dropping resistor wired to pin 13 and then a connection over here just brings back the ground connection and so without further ado let's plug it in and listen to our fabulous tune [Music] Shh [Music] and it plays over again now I'll be the first to admit that it's not quite the London Symphony Orchestra but as you can see this would make an amusing little Star Wars toy so now that you've seen our little Star Wars music box it's time to move the code down to an 80 mega 328 but before we do that I need to discuss one more thing with you and it's something called a bootloader now what exactly is a bootloader you may or may not have heard the term before essentially and this is a really simplistic explanation of this a bootloader is code that is already loaded on the tip and when you boot the chip up it loads this code and the code sets up a number of things it sets up the speed that these processor is going to run at it sets up a number of internal registers and it also prepares a chip to be able to receive commands over its serial line and to be programmed the centrally a bootloader is what the difference between a raw microcontroller and an Arduino is and the arduino bootloader is open source code that you can load onto the chip yourself another option however is to buy an 18 mega 3:28 with the bootloader already on it and that's what I've done today now I'm not going to demonstrate how to load a bootloader onto a blanket he made it a 328 simply because I don't have a blank 80 mega 328 without a bootloader I've checked and all of the ones I have in stock I already have the bootloader on it but you can buy an 18 mega 28 without the bootloader and load it yourself now if you want details on that the article that accompanies this video in the drôme BOTS workshop comm website explains this in some detail and also will give you links onto how to do it there are a number of methods to do it you can do it using an Arduino itself to load the bootloader onto a rod ship you can also buy special devices that are made for loading boot loaders and if you're mass producing things and you want to get the best value then this is the way to do it because tips without the bootloader are cheaper than those that already have the bootloader but again I've already got mine installed so I'm not going to go through this process in the video simply because I don't have a trip to demonstrate that with so if you are ordering an eighteen mega 328 one thing to note is that usually 18 mega 328p use I have the bootloader installed whereas if they don't have the PU designation at the end they probably don't have the arduino bootloader so with that in mind let's take a look at what's required to wire up our chip to emulate an Arduino and then we will see what we have to do to load our code on to it now here's the basic hook up for the 18 mega 3:28 in addition to the 18 mega 328 chip you will need a 10k resistor and a 16 megahertz crystal to 22 Pico farad capacitors as well as a 10 microfarad electrolytic capacitor this by the way is optional but it's a good idea to include it you'll start by wiring pin seven of the 80 mega 3:28 to the positive 5 volt line from your power supply then connect pin 8 of the 80 mega 3:28 to the ground side of your power supply you'll also connect pin 22 to the ground and pin 20 to the 5 volt supply now we will wire the reset line which is on pin 1 connect pin 1 to one side of the 10k resistor the other side connect that to 5 volts next we'll connect the crystal the pins 9 and 10 of the 18 mega 3:28 crystals are not polarity sensitive so you can put them either way around will also connect one side of the 22 Pico farad capacitors to each lead on the crystal the other side of the capacitors will be connected to ground finally we'll connect their 10 micro farad capacitor across the power supply lines make sure to observe the proper polarity here and this completes the wiring of our 80 mega 3:28 so here's my 18 mega 3:28 wired on to a solderless breadboard and you can see right now that I'm running the blink sketch what I did was I connected a resistor over to pin 19 and then from the resistor I connected an LED over to ground now pin 19 on the 18 mega 328 is equivalent to digital i/o pin 13 on an i arduino uno so as you recall from the blink sketch its purpose is to blink the LED that's attached the pin 13 and it's doing this right now I will show you in a moment of course how I got the code onto the 18 mega 3:28 now you can see some of my components here if you look carefully here's the crystal and the 222 Pico farad capacitors and over here is the 10k resistor that's tied to the reset line and otherwise I've got the voltage and ground lines connected accordingly I've also put a 10 micro farad capacitor across the supply line this is optional but it's a good idea it is to keep any noise off the power supply line and so there you go and Arduino on a breadboard and as you can see it's significantly smaller than a complete Arduino Uno and if I were to put this on to a perf board or a printed circuit board I could make it even tinier and I'm powering everything by the way off of the 5 volt power supply that I have on my workbench you'll need a good source of 5 volts in order to do these experiments you could use a USB power adapter if you have power adapter that you can run a couple of wires from and that would work just fine as well so now that we've seen this let's move on so now that we've seen how we can take an 18 mega 28 and wire it up with a crystal and a few capacitors and a resistor to emulate an Arduino we now need to know how we can take our sketch and load it onto the device in actual fact there are three different ways you can do this now the first two methods are going to require a particular style of Arduino I've got couple of our dwee nose down here on the workbench and this is the style that you want now I'm hoping you can see that this Arduino Uno uses a 28 pin dip or dual inline package and the tip is mounted in a socket and this is actually how the original Arduino Uno was built and several clones are built this way if you are using this type of an Arduino then I have two methods that you can use this Arduino in order to take the code from your Arduino IDE and load it onto the 80 mega 328 now if you don't have this style of Arduino and have this style instead now this is a style that many clones use they use a surface mount version of the chip and with this type of Arduino you're not going to be able to use the first two methods that I talked about so I have a third additional method that's going to require an additional component that you can use to load code onto the 80 mega 328 so with that said let's take a look at the first and by far the simplest method of taking the code that you write on your Arduino IDE and moving it on to an 18 mega 328 chip so I've taken the 80 mega 328 out of my Arduino Uno and I've inserted it on the breadboard now when you're taking the chip out there's a couple of things you can use this is a tip puller and it grabs the chip around the edges like this and you can use it the hall to chip up and it's kind of useful if you don't have something like that though you can just use a flathead screwdriver and just go under each end of the chip to lift it up now in both cases you want to be very careful not to bend the leads on the integrated circuits so be very careful when you do that take a look at the leads when you take it out that's also very true when you insert it back in to the Arduino Uno after you're finished with the experiment now otherwise I've taken the LEDs and the piezo and I've moved them over to my we know on a breadboard circuit over here with the 18 mega 3:28 and I'm going to be powering it once again using my bench power supply so again this chip has already been programmed in the uno and I've just simply moved it over here to the breadboard so let's put some power onto it and away we go okay so that is certainly the simplest way of doing it but probably not the best way of doing it so we'll look at another method in a moment so the first method of loading a sketch onto an 18 mega 3:28 was very simple obviously it's just exactly the same as loading code onto an Arduino and then just pulling the chip off there are of course some disadvantages to doing that the first one is that you end up with an Arduino that doesn't have a tip however if you've got a number of 18 mega 3:28 with bootloaders then you can just simply replace the tip and that's fine the second problem is that the socket on the Arduino clone or the Arduino Uno itself is not really made for repeated insertion and removal it will eventually start to degrade the contacts on it and as you may have noticed it's a bit difficult to get the chip out of the socket now one thing you can do is I've got this little shield over here and you can get similar fields to this this is a shield for the Arduino and as you notice it has as if socket on it and you take out the chip from your Arduino you pop the shield in and you can use this as a socket instead and so if you're doing multiple runs of a certain builder if you just want to program a bunch of different 18 mega 3:28 you can use something like this and it'll work fine but there is another method that I want to show you now again you need the style of Arduino Uno that has a 28 pin dip you can't use the surface mount one because you're going to need to remove it from the Arduino but instead of programming that tip and removing it you're just going to remove the chip put it aside and connect the Arduino up to the 80 mega 3:28 and I'll show you how to do that in a moment once it's connected you can actually use the Arduino Uno itself as a programmer so let's take a look at that method right now now here's how we wire things up in order to use an Arduino Uno to program an 80 mega 328 now you need to start off with the eight Mayda 3:28 hooked up exactly as I showed you earlier however I thought that all of these extra wires and connections would be a bit confusing on the diagram so I've chosen not to show them however keep in mind that they are there now we'll just move our 18 mega 3:28 to the side and we'll bring in an Arduino Uno and on the Arduino Uno we need to remove the 80 mega 3:28 from its socket now note that if you have an Arduino Uno that uses a surface mount version you won't be able to use this method once you've removed this processor from the Arduino Uno connect the reset line to pin one of the 18 mega 3:28 which is it's reset line then connect pin 0 which is the receive line to pin 2 of the 18 mega 3:28 and that's its receive line and finally the two transmitted lines are connected together pin 1 of the Arduino Uno to pin 3 of the 18 mega 328 you can also use the 5 volt and ground connections from the Arduino Uno to power the 18 mega 328 you no longer need an external power supply and now you'll be able to use the USB to serial converter on the Arduino to program the 80 mega 328 now here's my hookup using an Arduino Uno as a USB to serial converter now on the Arduino Uno this red line comes from the 5 volt output and so it's 5 volts is connected to the positive rail on the breadboard the black wire is the ground from the Arduino and it's connected to the negative rail this blue line goes from the reset output on the Arduino Uno and it's going to the reset pin on the 18 mega 3:28 now these are the transmit and receive outputs from the Arduino and pins 0 and 1 and they are connected to transmit and receive on the 18 mega 3:28 and the transmit is connected to transmit and receive is connected to receive because essentially we are just using the Arduino Uno as a front-end passed data over to the 18 mega 3:28 so I'm going to upload my code right now and you'll see a bit of activity on the lights and it's playing our tune [Music] [Music] now one thing I'm going to do is upload a blank sketch to it now and that basically clears the tip so we can use it for our next experiment now the methods we've looked at so far for programming an 18 mega 3:28 all relied on an arduino uno or uno clone that has this style that has the 28 pin dip chip and in the first one we just took the 28 pin chip and we took it out of its socket and used it on the breadboard and of course that worked pretty well the second method we also needed to remove the chip and we use the USB to serial converter which is this little tip on the board in order to program the 18 mega 3:28 on a breadboard but what happens if your Arduino Uno looks like this now if you have an you know clone that looks like this it uses a surface mount version of the 18 mega 3:28 and naturally you can't easily remove this and I'm not suggesting that you do you'll completely destroy the board and so there's another method you can use to program an 80 mega 3:28 on a breadboard that does not require an Arduino Uno at all and that's to use one of these these are FTDI adapters now these are both equivalent and function but there are different styles of adapters one interesting thing about these two different models is that the pin outs here are mirror images but what these are essentially are USB to serial converter x' and you can use one of these to program an 18 mega 28 so let me show you how we hook that up and then we'll program our chip on the breadboard using one of these FTDI adapters so here's how we're going to use an FTDI adaptor to program an 80 mega 3:28 once again we will start with a fully wired 18 mega 3:28 and as I did before I'm going to eliminate those connections from this diagram just so it doesn't get too confusing but keep in mind that they are there now in addition to the FTDI adapter you are going to need a 100 nano farad capacitor we will start by connecting the receive our RX line on the FTDI adapter to the transmit line on the 80 mega 3:28 that's pin 3 on the 80 mega 3:28 then we'll take the transmit line on the FTDI adapter and connect it to pin 2 of the chip which is the receive line next we will connect the DTR or data terminal ready line on the FTDI adapter to the reset line which is pin 1 on the 80 mega 3:28 if your FTDI adapter does not have a connection labeled DTR it may have one labeled RTS which means request to send and it performs the same function finally you can use the VCC connection and the ground connection from the FTDI adapter to power the 18 mega 3:28 now here's the set up by half with my FTDI adapter I've got the FTDI adapter and I've got the power and the ground leads from the ftdi attached to the power and ground rails on my bread board so I can power everything from the adapter I've got the transmit and receive lines connected to the transmit and receive on the 18 mega 328 and remember in this case transmit goes to receive and receive goes to transmit so it's backwards from the way that we connected the Arduino and this blue line over here is my data terminal ready or DTR line and I don't know if you can see it there's a 100 nano farad capacitor over here and from there I go over to the reset pin which still has the 10k resistor bringing it and so when I upload the code what I want you to watch is the lights on the FTDI adapter and so I'm gonna go and upload my code right now and you'll see the lights flicker as the codes being uploaded and it seems to be working [Music] may the force be with you and so that about wraps it up for today's video I hope that you enjoyed it I hope you learned something and most of all I hope that that's inspired you to take some of your Arduino projects and move them down to an 18 mega 2560 beta Belov whether this is the most economical solution but it certainly is the most basic solution the other thing is you can run an 18 mega the external crystal and the capacitor is assuming that you want to run it at a slower speed now if you'd like some information about doing that please check out the article on the website at the drone bots workshop comm where you'll find details and links above loading bootloaders onto an 80 mega 3:28 and also about using it in modes without the crystals and capacitors so if you're interested in that it's all in the article and while you're on the website please sign up for the newsletter if you haven't already my newsletter isn't a sales letter at all it's this simply a way of keeping in touch with you letting you know what's going on in the workshop and most importantly getting your opinion about the videos and articles that I create and about subjects that you would like to see me cover in the future so if you haven't done so already please sign up for the newsletter and if you haven't subscribed to the YouTube channel just click the link below the video and subscribe I would really appreciate that so until next time please take care of yourselves and I hope to see you very soon again in the workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 539,076
Rating: 4.9314666 out of 5
Keywords: ATmega328, atmega328p arduino, arduino uno, arduino (computing platform), star wars arduino
Id: Sww1mek5rHU
Channel Id: undefined
Length: 37min 17sec (2237 seconds)
Published: Sat Dec 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.