I am curious. I like to know how things work. For the curious, this Instructable will try to explain some of the Arduino electronics.
I suspect many people have built Arduino projects but couldn’t get them to work. I hope to provide some knowledge and skills to help fix hardware problems such as wiring errors and bad components.
After some Internet searching, I found an electronics tutorial that I like and trust:
"This tutorial is by Limor Fried”
In fact, I recommend it to all Arduino readers. There is some overlap of information but lots of multimedia and more on programming.
So who am I? I am a Lazy Old Geek (L.O.G.). I am not a degreed engineer but I’ve been around the electronics field for over 40 years starting as an electronics tech in the USAF. I spent many years testing electronic systems.
As most of you know, Arduinos are based on a little black chip called a microcontroller. This is the heart and brains of the Arduino. You don’t have an Arduino without one. There are many flavors of the Arduino microcontroller but they all have the same functions. Currently the most popular for the DIYers is the ATmega328. See picture.
Microcontroller: A microcontroller is a CPU (central processing unit) with memory and interface circuitry built-in to the chip. Basically, the CPU takes all the commands in the program (sketch) (.PDE) and does what they tell it to do. Notice that I said what the commands tell it to do; I did not say what the programmer wants it to do. Writing a successful program is telling the CPU exactly what you want it do in the language it knows.
Frequently asked: Yes, the ATmega328 can always be used instead of the ATmega168 as the hardware is exactly the same. The ATmega168 may not replace the ATmega328 as it has less memory so bigger programs may not work.
Technobabble: The rest of this section is for Geeks only.
1 K Bytes
What does this mean? The K stands for kilo, a multiplier which in this case, means multiply the number by 1024. Bytes are just a place to store information (data). This is basically the only difference between the two microcontrollers.
Flash: is a type of memory that holds program information, even after the Arduino is disconnected from power. The same program will run anytime power is reapplied to the Arduino. This is the same way USB flash drives and digital camera cards retain their information.
EEPROM: is memory that also retains information after power is shutoff. It is different from Flash as it can be written by the program instead of the program itself. The Arduino instructions to use EEPROM memory are EEPROM.read() and EEPROM.write().
Tip: Be sure to have:
//in your sketch(program).
The limitation is, even in the ATmega328, there is only 1024 bytes so only so much data can be stored. Tip: By the way, I never got this to work.RAM: is also memory but it is volatile meaning it will go away when you turn off power. Sketches use it to store temporary information such as variables. What is a variable? Well, it is something that can change. Examples are the temperature or the time of day. Here is part of a sketch that converts a temperature sensor reading to degrees C (Centigrade) and degrees F (Farhenheit). float Vt=(float) sensorValue3Avg*5.0/1023.0;
All of these float variables are stored in RAM and will be overwritten the next time a measurement is taken and are lost when power is turned off.
Basically, variables are just labels for locations in RAM. Float variables are a specific type of variable. With this label, the sketch knows where to go to store the value it wants or retrieve the value stored at that location. The specific type determines how much room is needed and how to interpret the information.
Besides the CPU, flash and RAM, the ATmegas have interface circuitry built in:
Serial interface: this allows the CPU to talk to the PC through a serial port or through USB and I believe it’s used to communicate over I2C. This is also how it talks to serial LCDs.
Analog to digital converter (ADC): This allows the ATmega to convert analog voltages to digital data (will be discussed in another Instructable).
PWM (pulse width modulation): circuitry to output ‘analog’ voltages
Timers: for timing events, most often used to set delays between program steps, e.g., blinking LEDs.
If you have ever looked at the datasheets on these ATmegas and understand them, then maybe you should be writing this instead of me.
Prefixes: There are a lot of letters attached to electronic terminology that may be confusing. E.g. 16mV, 10Kohms, 20uF. These letters are called prefixes (and suffixes) that are just multipliers to the value. E.g., 10Kohms is (10 times 1,000) ohms or 10,000 ohms. See the table below.
Non-essential Info: Unfortunately, if you’re talking computer memory 1Kbyte is 1024 bytes. This is because computer people like to make everything complicated. So to them K is 210. Megabyte can mean 1,000,000, 1,048,576 or 1,024,000. Don’t ask, check it out on Wikipedia.
Step 1: Basic Electronics
One of my earliest experiments in electricity was when I was about eight. I think we were studying electricity in Cub scouts. We were studying about voltages and loads and complete circuits. So I had a flash of brilliance (to bad it wasn’t a flashing warning light). I figured for a complete circuit all I needed was a wire so I took the two ends of the wire and plugged them into an AC outlet. Kaboom! I sure did have a flash of brilliance. In truth it was a complete circuit, however my dad didn’t see it that way. DO NOT TRY THIS ONE!
Now this is an unusual way to approach electronics but I think it will make it easier to understand, eventually.
Complete circuit: For anything to happen electrically, you have to have a complete circuit. This is a fundamental principle. A good example is a flashlight. The first drawing shows a complete circuit. A complete circuit consists of a power source (battery), a load (lamp) a connection from the power source to the load (white wire) and a connection from the load back to the power source (black wire). Electricity has a path from the voltage to the load and from the load back to the source making a complete circuit.
So why do I use this concept of complete circuits? I like it because you can focus on what you are concerned about and not worry about the rest of the circuitry. If you have a problem, say an LED isn’t blinking, then you can concentrate on the components that are needed to make that LED light up and not worry about everything else.
Some complete circuit examples in the Arduino world:
Power: One complete circuit could be a 6V battery connected to the Vcc pin of the ATmega(the load) and the ATmega ground pin connected back through the ground to the other side of the battery. See picture.
Okay, so there may be a few questions.
Some may ask: what does this picture mean. Well, this is a schematic and I will get into that in a minute.
Others will ask: how can you power an Atmega chip with 6V. Technically, this Atmega can be powered from 1.8Vdc to 6Vdc so this would work.
Others may ask: how can this be a complete circuit? It doesn’t do anything. It is true it doesn’t do anything but it is also a complete circuit. Who cares you may ask.
The answer is without this complete circuit, the Arduino will not work.
Schematic: A schematic is a pictorial representation of an electronic circuit. Remember that Atmega328 microcontroller, I showed you in the last step? Will I put the picture in this step also. See the little metal legs sticking out? Well, there are 14 of these pins on this side and 14 on the other. The pins are numbered from 1 to 28. On the left side of the chip, you can see a little half moon cutout. That means the pins under the white dot is 1 and they continue in sequence in a counter clockwise direction. The next picture shows a pictorial of the pins.
So you may ask: why aren’t they labeled 1 thru 14 on the left and 28 thru 15 on the right, like in the pinout drawing? Well, some schematic symbols will do that. This one is setup more for functionality. The critical thing is what is connected to what pin number.
Technobabble: So what is that little circle on pin 1? Well, that means negative logic. Negative logic means that it happens when the signal is zero volts. Inside the box, it says /RESET. That means that the Atmega goes into reset when pin 1 is low (0V). The little slash (/) before the RESET means the same thing. You may also see a ‘RESET’ with a line over the top of it. That means the same thing. So whenever pin 1 is low, the AtMega is in RESET and cannot do anything.
So the little green lines on this schematic represent wires like the white and black wires of the battery-lamp complete circuit.
The symbol on the left is a battery cell. The side with the shorter line is negative and the longer is positive.
In summary: the complete circuit is the positive side of the battery connected to the VCC pin of the AtMega. The ground pin of the AtMega connected to the negative side of the battery.
Step 2: More Complete Circuits and Schematics
Okay the first schematic in this step is another complete circuit but is more like what you would see. VCC is the positive side of the power supply and GND is the negative side. You can visualize it like a battery if it’s easier for you. The power supply will be discussed in another step.
The second schematic adds a couple of connections.
Schematic tip: Whenever you see a label, like VCC more than once on a schematic, that means they are tied together. So VCC is connected to both pin 7 and pin 20. GND or Ground (same thing) is connected to pin 8 and pin 20.
Technobabble: While technically, you don’t need to connect grounds to both 8 and 20, it is standard practice to do so. A couple of reasons are current carrying capacity and RF noise.
AVCC is different than VCC. VCC powers the AtMega chip. AVCC is power for the Analog circuitry of the chip. Since we’re not using Analog circuit at this stage, it could be left off but it is easier to connect it always rather than try to figure out why something isn’t working right later.
The third schematic is electrically equivalent to the second but just drawn differently.
Schematic Tip: Generally, when you see a dot on a wire line it means they are connected. If two lines cross without a dot, then there is no connection.
So if you built one of these circuits, it would be complete but it wouldn’t do anything. But these are vital connections for it to do anything.
Step 3: Functional Complete Circuits
So the first schematic adds a resonator. A resonator (or a crystal) is like a heartbeat for the microcontroller. Microcontrollers are dumb. See my Instructable:
They have to be told what to do and when. The when is determined by a clock cycle. Many of you know that most AC power has a frequency of 50 or 60 cycles per second or in electrical terms, 60 Hertz (Hz). Most AC clocks use this frequency to keep time. Well, microcontrollers need a clock to keep it operating.
Simplified: A voltage alternates between high and low continuously. So the microcontroller will move to the next step with the next cycle. The AtMega is 16Mhz or 16 Million cycles per second. That seems like a lot but it’s slow in comparison to modern PCs which run at 3GHz. (Look it up if your curious)
Technobabble: Well, technically, the AtMega has an internal clock that runs about 8MHz, but the Arduino does not easly support it.
The next picture is a resonator.
The next schematic shows a crystal oscillator and two capacitors which do the same thing as the resonator. The capacitors help the oscillator oscillate. Capacitors come in different sizes and shapes. The 22pF is the value of the capacitor, in this case 22 picoFarads. I will discuss capacitors a little more under the Power Supply section.
So you ask: why use a crystal when the resonator is smaller, cheaper and one part instead.of three? Well, that is a good question. The resonator frequency is a little less accurate. For most Arduino applications, this is not a problem, so in most situations, the resonator is a better choice. So for certain time critical applications, the crystal and caps may be needed.
Technobabble: The labels on pin 9 and 10 say xtal which is an abbreviation for crystal.
The next schematic adds a resistor from VCC to pin 1. While I believe the Arduino may work without this resistor, it is good electronic design practice to not leave any input pins floating.
Resistor: A resistor is a part that limits the amount of current that is flowing in a circuit. Analogy: If you are running water in a garden hose and you pinch the hose, less water flows. This is like a resistor. In my little experiment of sticking a wire in the AC socket, if I would’ve used a big resistor, it wouldn’t have blown up. It would have gotten hot though.
DO NOT TRY THIS!!
INFORMATION: The squiggle line is the American symbol for a resistor. Europeans use a small rectangle.
Resistors come in many sizes and shapes. The next picture shows a common version and it shows you what the color bands mean. The higher the resistance, the more restricted the flow.
Technobabble: If you've read all of this, I talked about pin 1 being /Reset. If the AtMega is in reset, then it cannot do anything. The resistor pulls the pin up to (almost) 5 volts so that it isn't in reset.
Technobabble: If you've read all of this, I talked about pin 1 being /Reset. If the AtMega is in reset, then it cannot do anything. The resistor pulls the pin up to (almost) 5 volts so that it isn't in reset.
So now, we have the Arduino sitting there, powered up and clocking along but still not doing anything.
Step 4: Power Supply
Well, I am going to take a slight detour. I am sure all the readers are dying to know where the VCC and GND come from to complete all of these circuits. Well, here goes.
The first picture is a standard Arduino. Notice in the lower corner there is a connector for a wall power supply. Many users will plug in a power supply into this connector. The power supply will typically be about 9Vdc.
The next picture, schematic, is the typical power circuit for an Arduino.
The rectangle on the left is the black connector. Notice that two connections, 1 and 3 are connected to ground. Pin 2 is the 9Vdc.
The next device, the triangle with the line labeled D1 is called a diode. A diode will only let current flow in one direction. This is a protection diode. If you connected the wrong power supply and negative voltage was on pin 2. This diode would keep the negative voltage from damaged the Arduino.
The next device is C6, an electrolytic capacitor. Note the little + sign on the top. This capacitor and most you will find in Arduino circuits are filter capacitors. Microcontrollers are basically digital devices. Digital devices try to switch states from high to low and low to high, in this case 5Vdc to ground, instantly, this will add noise on the 5Vdc. Instead of a steady 5Vdc, the voltage will fluctuate, higher and lower. The electrolytic capacitors will smooth out slow fluctuations. Analogy: This is like a water reservoir. If there is no reservoir and you try to take some water out, the water level will drop. If you have a reservoir, the water level won’t drop as much.
C5 is a much smaller valued capacitor. It is also a filter capacitor for faster fluctuations.
The next device, the rectangle is a 5Vdc voltage regulator. A common one is a 7805. What this does is drops the 9Vdc input on the input side to 5Vdc on the output. The difference is actually lost as heat.
C7 is a filter capacitor for the 5Vdc. In this schematic, instead of using VCC, it uses +5V.
The next two components, R2 and LED1 are actually a separate complete circuit. The complete circuit is +5V to the resistor to the diode to GND and thru the power supply back to +5V.
Note: The resistor symbol is the European version. The LED looks like a diode and acts like one. The difference is the little arrows which means that it will light up when current flows in the right direction.
The next picture is an electrolytic capacitor. They can be installed backwards, bluish bad with the – signs is the negative, the opposite of the + in the schematic.
The next picture is a 7805 voltage regulator. The pins are not labeled and you often research the internet to get the correct pinouts.
The next picture is a typical diode. These can also be installed backwards, the black band is the line side as opposed to the triangle side.
The next picture is a typical LED. The flat side of the round plastic is the ‘cathode’ side, the line as opposed to the triangle.
Technobabble: So, the ideal digital circuit transitions from 5Vdc to 0Vdc, instantly. In the real world, this is impossible but the closer you can get the better. Now this ideal circuit is basically a square wave. From a physics point of view, a square wave is made up of all frequencies. In the physical real world, that is impossible, thus so is the instant transition.
Electrolytic capacitors are typically in the uF, microFarad range, say 1 to 1000uF. These are better for filtering low frequencies, say 50-1000Hz.
The ceramic capacitors, like C5 are in the pF (picoFarad) or nF (nanoFarad) range are better at filtering higher frequencies, maybe 10,000Hz +.
Step 5: Alternate Power Supply
Quite often, an Arduinoite will connect their Arduino to a USB port on a computer usually to load up a sketch. Many may not realize this will also supply 5V to the Arduino and they do not need to have the other power supply connected.
The next partial schematic shows how the Arduino UNO uses USB power. This similar to the way other USB Arduinos work. The PC(personal computer) USB connector has 5V on pin 1 and Ground on pin 4.
The USB connector is on the lower left side of the schematic. Follow the Ground on pin4 labeled UGND. It connects to that little split circle that connects to GND. Now I don’t have a UNO but I think that split circle is a splice that is shorted across. So the USB ground is connected to the UNO ground.
The 5V side, USB pin1 is a little trickier. It goes thru F1 which is a 500mA fuse. A fuse is basically a wire that will break apart if more current than it’s rating passes through it. Coming out of F1 (still 5Vdc) is the connection labeled USBVCC which is connected to a matching USBVCC which is shown in the second schematic. This is connected to a FDN304 which I believe is a P MOSFET. A P MOSFET is an electronic switch.
Technobabble: I think the following is correct. If a power supply is plugged in, then voltage on VIN is about 9 V. The two resistors form a voltage divider which makes CMP about 4.5 V. The other pin of the comparator, U1A is at 3.3V. so the output is high. This voltage on the P MOSFET shuts it off so USBVCC goes no where. But 5V is supplied by the power supply from the last step.
Now when there is no power supply connected, Vin is 0V and RN1A brings CMP down to OV so the output is low and the P MOSFET passes USBVCC to the +5V and powers the rest of the Arduino.
To summarize, if an external power supply is connected, it supplies the +5V. If the USB is connected and there is no power supply connected, then the USB supplies the +5V.
Tip: Some of the older Arduinos have a jumper connection where you the user has to select the power supply source. if your circuit isn't working, check that this jumper is where it should be.
Tips: The USB can supply a maximum of 500mA of current. If your circuit requires more than that, then it probably won’t work very well. You might need the other power supply.
Due to varying circuit designs, the 5V on the USB may not be 5V. It might be around 4.75V. This could be a problem in some circuits. I will try to explain this in a later step.
Step 6: Working Arduino
Ok. Now we’re ready to do something. So we add a resistor to AtMega328 pin 19 and an LED to ground.
Let’s follow the complete circuit in this schematic. VCC goes to AtMega pin 7. AtMega pin 19 connects to R2 which connects to LED1 which connects to Gnd and back to VCC. A complete circuit right?
Not quite. How does the signal get from pin 7 to pin 19?
Okay, we have to tell the AtMega to connect pin 7 to pin 19. So how do we do that?
That is where the Arduino sketch, blink.pde comes in. It is an integral part of the complete circuit.
Now I am not going to go into sketches, but . . .
TIP: If you ever looked at blink.pde, it refers to the LED being on pin 13. How can that be? It is connected to pin 19. Well, there’s a little gotcha that Arduinoites don’t think of pin numbers as referring to the AtMega, they have their own numbering system and we’ve already seen it. It is the Arduino Pin Map.
Now the Digital pins 0 through 13 are referred as 0 through 13.
The Analog Inputs 0 through 5 are referred to as A0 through A5.
Actually, most Arduino PCBs, like the UNO, the Duemilanove, the RBBB, the Freeduino. will have connector pins labeled with the D0-D13 and the A0-A5. However, if you are building an Arduino on a breadboard or protoboard or your own PCB, then you have to be aware of this.
Tip: A little known fact is that the AtMega328 surface mount chips have two extra Analog pins. I was told that the Arduino software supports these as A6 and A7. I’ve never verified this. The Arduino UNO SMD doesn’t even have connections to these pins. However, I have an Seeeduino V3 with SMD and it actually has two extra pins labeled ADC6 and ADC7 on their shield connector. See picture. They shouldn't interfere with any shields. So if you ever need more than six Analogs they are available. . .
Summary: Well with this Instructable, you should theoretically be able to troubleshoot the basic Arduino. Now if you are connecting any other hardware to your Arduino, I can’t be of much help but if you break it down to simple complete circuits, it should be a lot easier to figure out.