INTRODUCTION TO MICROCONTROLLERS
What are microcontrollers? They are what their name suggests. Today they can be found in almost any complex electronic device – from portable music devices to washing machines to your car. They are programmable, cheap, small, can handle abuse, require almost zero power, and there are so many variaties to suit every need. This is what makes them so useful for robotics – they are like tiny affordable computers that you can put right onto your robot.
Augmented Microcontrollers and Development Boards
In a pure sense, a microcontroller is just an IC (integrated circuit, or a black chip thing with pins coming out of it). However it is very common to add additional external components, such as a voltage regulator, capacitors, LEDs, motor driver, timing crystals, rs232, etc to the basic IC. Formally, this is called an augmented microcontroller. But in reality, most people just say microcontroller even if it has augmentation. Other abbreviations would be ucontroller and MicroController Unit (MCU). Usually when I say microcontroller what I really mean to say is augmented microcontroller.
As a beginner it is probably best to buy an augmented microcontroller. Why? Well because they have tons of goodies built onto them that are all assembled and debugged for you. They also often come with tech support, sample code, and a community of people to help you with them. My microcontroller parts list shows the more popular types that you can buy. They tend to cost from $30 to $150 depending on the features. This will give you a good introductory to microcontroller programming without having to be concerned with all the technical stuff.
In the long term however you should build your own augmented microcontroller so that you may understand them better. The advantage to making your own is that it will probably cost you from $10-$30.
Between getting a full augmented board and doing it yourself is something called a development board. These boards come pre-augmented with just the bare basics to get you started. They are designed for prototyping and testing of new ideas very quickly. They typically cost between $15 and $40.
What comes with the IC?
There is a huge variety of microcontrollers out on the market, but I will go over a few common features that you will find useful for your robotics project.
For robots, ore important than any other feature on a microcontroller, is the I/O ports. Input ports are used for taking in sensor data, while output is used for sending commands to external hardware such as servos. There are two types of I/O ports, analog and digital.
Analog Input Ports
Analog Ports are necessary to connect sensors to your robot. Also known as an analog to digital converter (ADC), they recieve analog signals and convert them to a digital number within a certain numerical range.
So what is analog? Analog is a continuous voltage range and is typically found with sensors. However computers can only operate in the digital realm with 0s and 1s. So how does a microcontroller convert an analog signal to a digital signal?
First, the analog is measured after a predefined period of time passes. At each time period, the voltage is recorded as a number. This number then defines a signal of 0s and 1s as shown:
The advantage of digital over analog is that digital is much better at eliminating background noise. Cell phones are all digital today, and although the digital signal is less representative than an analog signal, it is much less likely to degrade since computers can restore damaged digital signals. This allows for a clearer output signal to talk to your mom or whoever. MP3s are all digital too, usually encoded in 128 bit. Higher bit rates obviously mean higher quality because they better represent the analog signal. But higher bit rates also require more memory and processing power.
Most microcontrollers today are 8 bit, meaning they have a range of 256 (2^8=256). There are a few that are 10 bit, 12 bit, and even 32 bit, but as you increase precision you also need a much faster processor.
What does this bit stuff mean for ADC? For example, suppose a sensor reads 0V to an 8 bit ADC. This would give you a digital ouput of 0. 5V would be 255. Now suppose a sensor gave an output of 2.9V, what would the ADC output be?
Doing the math:
- 2.9V/5V = X/255
X = 2.9*255/5 = 148
So how do you use an analog port? First make sure your sensor output does not exceed your digital logic voltage (usually 0V -> 5V). Then plug that output directly to the analog port.
This bit range could also be seen as a resolution. Higher resolutions mean higher accuracy, but occasionally can mean slower processing and more succeptability to noise. For example, suppose you had a 3 bit controller which has a range of 2^3=8. Then you have a distance sensor that outputed a number 0->7 (a total of 8) that represents the distance between your robot and the wall. If your sensor can see only 8 feet, then you get a resolution of 1 bit per foot (8 resolution / 8 feet = 1). But then suppose you have an 8 bit controller, you would get 256/8=32 ~ 1 bit per centimeter – way more accurate and useful! With the 3 bit controller, you could not tell the difference between 1 inch and 11 inches.
Digital I/O Ports
Digital ports are like analog ports, but with only 1 bit (2^1=2) hence a resolution of 2 – on and off. Digital ports obviously for that reason are rarely used for sensors, except for maybe on/off switches . . . What they are mostly used for is signal output. You can use them to control motors or LEDs or just about anything. Send a high 5V signal to turn something on, or a low 0V to turn something off. Or if you want to have an LED at only half brightness, or a motor at half speed, send a square wave. Square waves are like turning something on and off so fast that its almost like sending out an analog voltage of your choice. Neat, huh?
This is an example of a square wave for PWM:
Also a quick side note, analog ports can be used as digital ports. <!–
Usually there will be about 8 I/O ports, timers, triggers, power lines, interrupt pins,
serial connection pins, a set amount of ROM (to store the program, around 50kb)
and RAM (to store data for variables and such, around 10kb), and a CPU speed of about ~20MHz.
There are many different types of microcontrollers out there with various features, so just make sure
it can do what you need. Know that size does not mean more power. Concerning electronics,
men brag about who has the smaller cellphone😉
Most MCUs are available in a plethora of
different variations, each appealing to different user requirements. If you dont need much I/O,
you can get an MCU in an 8-pin DIP package. If you need more ports, essentially the same CPU
is available in larger packages, and with other peripherals.
Note that a bit is represented
by a set voltage. A 0 would be 0V, and a 1 would usually be 5V. For analog you have a
range between 0V and 5V.
Serial Communication, RS232, UART
A serial connection on your microcontroller is very useful for communication. You can use it to program your controller from a computer, use it to output data from your controller to your computer (great for debugging), or even use it to operate other electronics such as digital video cameras. Usually the microcontroller would require an external IC to handle everything, such as an RS232. To learn more, read my microcontroller UART tutorial.
A timer is the method by which the microcontroller measures the passing of time – such as for a clock, sonar, a pause/wait command, timer interrupts, etc. To learn more, read my microcontroller timer tutorial.
I^2C (pronounced I-squared-C) is also useful for communicating, but I have never used it. Just make sure your controller has some method of communicating data to you for easy and effective debugging/testing of your robot programs. Its actually somewhat complicated, but usually the manufacturer has simplified it so all you have to do is plug-n-play and do a few print statements. To learn more, read the I^2C tutorial.
To run a DC motor you need to either have an H-Bridge or a Motor Driver IC. The IC is great for small robots that do not exceed 1 or 2 amps per motor and the rated motor voltage is not higher than about 12V. The homemade H-Bridge would need to be used if you wanted to exceed those specs. There are a few H-Bridge controllers commercially available to buy, but usually they are way too expensive and are designed for battlebot type robots. The IC is small, very cheap, and can usually handle two motors. I highly recommend opting for the IC. Also, do not forget to put a heatsink onto the motordriver. Motordrivers give off pretty fireworks when they explode from overheating =)
Another interesting note, you can stack ICs in parallel to double the allowable current and heat dissipation. Theoretically you can stack as many as you want, as long as the current is high enough to still operate the logic of the IC. This works for voltage regulators too.
Im referring to anything that can be used for debugging by communicating information to you. LEDs, buzzers, LCD screens, anything that gives output. The better the indicator, the easier the debugging. The best indicator is to have your robot tethered and print or data log sensor and action data to your computer, but it isnt always possible to have your robot tethered.
The lowest form of programming languages is the machine language. Microcontrollers need to be programmed with this.
An example of machine language:
- 3A 10 51
32 38 00
Obviously neither of us could ever memorize what all those seemingly random numbers and letters do, so we would program in a higher language that makes much more sense:
- If (language = = easy)
These higher languages would then be compiled automatically into a machine language, which then you can upload into your robot. Probably the easiest language to learn would be BASIC, with a name true to itself. The BASIC Stamp microcontroller uses that language. But BASIC has its limitations, so if you have any programming experience at all, I recommend you program in C. This language was the precurser to C++, so if you can already program in C++, it should be really simple for you to learn. What complicates this is that there is no standard to programming microcontrollers. Each has its own features, its own language, its own compiler, and its own uploading to the controller method.
This is why I do not go into too much detail because there are too many options out there to talk about. The support documents that come with the controllers should answer your specific questions. Also, if you decide to use a PIC, understand that the compiler program (at least the good ones) can cost hundred of dollars. Most microcontrollers also require a special interface device between your computer and the chip for programming which could also cost from $10-$40.
With possibly the exception of DC motors, the microcontroller is the most expensive part of your robot. There is just no escaping the costs, especially for the beginner. But remember, after buying all this for your first robot, you do not need to buy any of it again as you can reuse everything. So here is the breakdown of costs. The chip itself, without augmentation, would only cost dollars. But understand the chip is useless without the augmentation, so you would need to do it yourself if you do not buy it already augmented. This could potentially cost just as much with the augmentation, and could cause you many frustrations.
If however you are more experienced (and for some odd reason still reading this), you can customize your own circuit to do exactly what you want. Why have a motordriver when you are only using servos anyway? If you decide to buy an augmented MCU, the cost will range from about $50-$150. To compile your program, you would need to get special compiling software. Atmel and BASIC Stamps have free compilers. PICs however have fairly expensive compilers. There are some free ones available online, but they are of poor quality in my opinion. CCSC PIC C compiler is about $125, but I think it is worth getting if you are going to use PICs.
You will also need an uploader to transfer the program from your computer to the chip. This generally requires more special software and a special interface device. The Cerebellum PIC based controller has this built in which is really nice and convienent, but for any others expect to spend from $10-$40. People often opt to just make their own as the circuit isnt too complicated.
As a prototyper, what you probably want most is a MCU development board. These augmented microcontrollers are designed for the prototyper in mind. To find these augmented MCUs, do a search for pic development board, atmel development board, stamp development board, etc.
If you have more specific questions about microcontrollers, or would like me to go into more detail about something, just write me and I will.
Ive created a microcontroller product, called the Axon, thats both easy to learn and powerful in features. I use it for all my robot creations now, and will continuously release source code updates and tutorials using it. Feel free to check it out!
Entry filed under: Microcontrollers. Tags: .