Sunday, January 5, 2014

The Power of Arduino


Subscribe by Email!
...And how you can learn to use computer programming to control and customize the physical world around you, including your RC vehicles...

Subscribe to and share this blog by clicking the links to the right -->

By: Gabriel Staples
Written: 5 Jan. 2014
Last Updated:  15 Apr. 2017
History (newest on TOP):
-updated Amazon links & prices, added Elegoo products, and converted Amazon links to Amazon Affiliate links - 15 Apr. 2017
-added link to (incomplete, but functional) touch lamp code - 20 Oct. 2016
-added info on __attribute__ syntax - 25 Sept. 2015
-added more __attribute__((__packed__)) and #pragma pack(1) links to bottom - 4 Sept. 2015
-macro comment addition to Intermediate section - 20 June 2015
-added Expert links section - 9 May 2015
-links edited at bottom - 5 March 2015
-added another Advanced link - 24 Feb 2015
-added Vilros starter kit links to bottom - 7 Feb 2015
-added source code link for my RC car read PWM sketch - 2 Nov. 2014
-added a bunch of new links at bottom of article (mostly Intermediate) - 14 June 2014
-added details and corrections pertaining to microcontrollers being used in servos - 17 March 2014

====================================================================
Be sure to check out the links and resources at the end of this article, as they are EXTREMELY useful to anyone who uses Arduino microcontroller development boards!
====================================================================

Other Related or Interesting Articles:
One Good Use of an Arduino: Use it as a Touch Lamp controller [source code]:

Background:
So, you may be wondering why I haven't written an article in the last few months.  To be honest, writing a well-informed article, with plots, pictures, and sources takes vast amounts of time, and life gets busy.  However, I should also tell you that I have been heavily involved in Arduino microcontroller programming for the last several months, and I have spent countless hours learning about and programming Arduino.  I have now built up a variety of Arduino tools and resources that I use, including dozens of bits of test code, many projects and programs, and hundreds of files, links, and resources.  I have even written some code and functions which I have seen nowhere else, which I think are very valuable, and which I intend to periodically share here on my blog.  In other words, I think I have some useful things to contribute to the vast world of Arduino, some of which will help bridge the world between Radio Control, robotics, and Arduino microcontroller programming.  Though I am constantly learning, and constantly seeking help from resources and people who know much more than I, I would consider myself a very informed Arduino user with a sound, yet constantly growing, understanding of electricity, electrical engineering, programming, and computer science.

Why have I recently been doing Arduino programming instead of doing Radio Control stuff?

Learning Arduino is doing Radio Control stuff.  It turns out that the world of Radio Control is
completely inundated with microcontrollers and programming--though perhaps very few Radio Control guys/gals are aware of this.  A microcontroller is, in short, a miniature computer, which operates as intimately with hardware as is possible for code to do.  It is designed to control the physical world around us.  RC devices are controlled by them.  The Arduino development board utilizes Atmel-brand microcontrollers.  The Arduino therefore is an incredibly capable little computer which makes customizing your RC experience, adding additional sensors or functionality, or even turning your RC device into an Unmanned Aerial Vehicle (UAV), Unmanned Aerial System (UAS), or Unmanned Ground Vehicle, possible!  With Arduino, you can take a standard RC device and make it an autonomous or semi-autonomous robot.  The limit of what you can do is only constrained by your ingenuity and imagination!

Microcontrollers in Radio Control Today
Without microcontrollers, like those used by the Arduino development platform, RC as we know it today would not exist!  Though I have become knowledgeable on the subject very quickly, I have been doing Arduino microcontroller programming for less than a year.  It wasn't until approximately April 2013 that I first picked up an Arduino and made it blink, and it wasn't until approximately July 2013 that I actually caught the vision.  Now it's time for you to catch the vision too!

When I fly an RC airplane, here's where microcontrollers are used:
  1. The transmitter (Tx): I know for a fact my computer-based Tx uses at least one microcontroller.  That's the "computer" in the radio that stores and changes settings, shows your settings on a display, and creates the PPM (Pulse Position Modulation) signal which comes out the trainer port in the back of the radio.  I don't know for sure, but I believe there is also an additional microcontroller in the RF (Radio Frequency) module which actually sends the signal out over the airwaves.  I presume that if this one exists in the RF module, it would be what controls the pseudo-random 2.4Ghz frequency hopping algorithm.
  2. The receiver (Rx): Every one of my 2.4Ghz Rx's uses a microcontroller to read in the signal, process it, separate the channels, and send out individual channel signals as PWM (Pulse Width Modulation) signals.  These PWM signals are what go to the servos and ESC (Electronic Speed Controller).
  3. The Electronic Speed Controller (ESC):  Every ESC must have a microcontroller.  Brushed speed controllers use one to chop the battery voltage via PWM, in order to throttle the motor.  Brushless speed controllers use one to chop the battery voltage via PWM, in order to throttle the motor, as well as to do a very complicated commutation algorithm, based on feedback, which times the switching and "rotating" of electrical signals around the motor in order to allow the electromagnets to push and pull on the permanent magnets, in just the right places and at just the right times, in order to cause the motor to rotate. 
  4. The servos (some).  Though many servos use integrated circuits (ICs), some servos have an embedded microcontroller chip to decode the incoming PWM signal from the Rx, read a potentiometer to decipher their position, and move themselves to a commanded position, using potentiometer feedback, to ensure they get there.  This is what servos must do, and though the servos I have looked at use an Integrated Circuit (IC) like the AA51880 Servo Motor Controller, some more advanced servos are programmable and have customizable gains, slew rates, and other control parameters, and use embedded microcontrollers rather than ICs.  One such servo is the open-source "OpenServo."
  5. The charger.  Every smart charger uses a microcontroller.  It is safe to assume that if it has a display, it uses a microcontroller.
  6. Your voltage checker and/or low voltage alarm.  I use a voltage checker on the ground to verify battery voltage and cell balance before and after each flight.  It uses a microcontroller.  I opened it up and checked.  I also use a low voltage alarm on my plane to beep when my battery is low.  It uses one too.
So, each and every time I fly an airplane, there are at least 5 places microcontrollers are used.  Each device that uses a microcontroller must have specially-designed code on it to perform very specific tasks, that a computer programmer (or someone like you or me) put there!  In a single flight, I may be using up to 10 or more (for sure I am using at least 5) microcontrollers just to make it happen.  Now, with the power of Arduino, I can learn to customize and enrich my RC experience by turning my RC devices into robots, by creating custom code (firmware) to load onto an Arduino and put on my airplane or car in order to make it do...whatever I want! 

Arduino video: Reading the throttle signal from an RC car receiver, and using it to change the blink rate of an LED (could be used as a head-lamp or tail light on the car, for instance): [source code]



My Arduino History:
  • Sometime during the beginning of 2013: My good friend Michael Cardoza, a high school Spanish Teacher (and great person to inspire one to try new Arduino and electronics projects), showed me his Arduino.  I had no idea what that was.  I wasn't very interested.  Didn't look very impressive, and to me, it wasn't related to RC (boy was I wrong!).
  • 13 April 2013: I made a used Arduino Duemilanove blink.  It took me ALL day on Saturday to accomplish this (prob. 8~10 hrs.).  This was a very rough transition into microcontroller programming, as the stinking thing was used and had a bad bootloader.  I eventually figured out how to reload the bootloader onto it using a USBasp AVR programmer I had picked up earlier from HobbyKing, but never used until that day. (By the way, the USBasp programmer uses....guess what....a microcontroller! :) ).
  • July 2013:  I picked up the Arduino again.  This time, I caught the vision.
  • By October 2013: I had become completely absorbed in Arduino microcontroller programming, as a new primary hobby for the time being (it will always be part of my RC endeavors now).  I began doing various projects and things, some being quite complicated and difficult for me, to continue learning Arduino.
  • Today (5 Jan. 2014): I continue making leaps and bounds, as able, in my understanding of and ability to use Arduino microcontroller programming. 
To date (13 April 2013 to 5 Jan 2014), I estimate (via some calculations I just did) that I have spent approximately 472 hrs. learning about and/or doing Arduino microcontroller programming or learning about and doing related circuit analysis and electrical engineering.  Arduino and microcontrollers are such an amazing tool.




A Few Helpful Arduino Resources
- BE SURE TO CHECK THESE OUT - these are really fantastic learning sources!


Arduino Starter Kits:
Learning Links

1-Beginner:
  1. http://arduino.cc/en/Main/Products - it is very important that you thoroughly understand your Arduino --learn all about your particular Arduino board here!
  2. http://learn.adafruit.com/category/learn-arduino - absolute beginner tutorials--very fantastic website and information!
  3. http://arduino.cc/en/Tutorial/HomePage - hundreds of beginner tutorials and examples
  4. http://arduino.cc/en/Reference/HomePage - Arduino reference page -- you will come here often!  If you aren't regularly referencing these pages, you should be!  You can also find the entire Arduino Reference right on your computer by going to Help --> Reference right inside the Arduino IDE (Integrated Development Environment).  This is SUPER useful when working offline.
  5. http://forum.arduino.cc/ - the Arduino forum - ask your questions and get help here
  6. Programming Arduino Getting Started with Sketches, By Simon Monk, $11 - GREAT BOOK; also teaches the basics of writing your own Arduino libraries.
2-Intermediate:
  1. Programming Arduino Next Steps: Going Further with Sketches, By Simon Monk, $14 - GREAT BOOK; among many other things, teaches some more advanced input/output and signal processing techniques for Arduino, as well as how to write your own Arduino libraries.
  2. http://playground.arduino.cc/Main/LibraryList - hundreds of user-contributed Arduino Libraries to expand what you can do with your Arduino!
    1. Libraries I have contributed have names which begin with "eRCaGuy," so do a search for "eRCaGuy" on this page and you'll see my libraries.
  3. http://playground.arduino.cc//Main/InterfacingWithHardware - tons of user-contributed code and examples to interface with hardware
  4. http://playground.arduino.cc//Main/InterfacingWithSoftware - tons of user-contributed code and examples to interface with software (including data-logging and plotting, live data display, controlling your Arduino via a GUI on your computer, etc)
  5. http://www.cplusplus.com/Arduino is based on C/C++.  As you become an intermediate user, you need to know that you are NOT limited just to the functions on the main Arduino Reference page.  Rather, you have access to nearly *any* standard C/C++ function. 
  6. AVR Libc reference pages - This link contains a list of all of the C functions (organized by header files, .h files) that are available for use on ATmega microcontrollers/Arduino!  
    1. For a real description, & examples, of how to use one of these functions, however, I recommend simply Googling the function name you want, as follows: ex: I want to know how to use "scanf," so I Google "scanf c", and the first link that pops up is this: http://www.cplusplus.com/reference/cstdio/scanf/.  
      1. Or, let's say I don't know that "scanf" exists, but I want to know how to read parts of a string, out of a string.  So, I Google "c read parts of a string", and the first link I find is this: http://bytes.com/topic/c/answers/221802-how-read-part-string.  Now, I read their code and see that "scanf" is used in their code, so I can either look for the reference page for this function on Cplusplus.com directly, *or* I can Google "scanf c", as described above, to find a good reference page describing it in detail.
    2. Alternatively, you can simply go directly to http://www.cplusplus.com/, or other similar C/C++ websites, and search for the function you want to use.  Again, an Arduino can use virtually any C/C++ function, since that's what its language is based on!
    3. If any C/C+ function you find does *not* work on Arduino, or acts strangely, however, go back to reference the AVR-libc pages, as AVR-libc is what actually implements the C/C++ language for Atmel's AVR microcontrollers, and not 100% of the functions are implemented. Some deviations may exist. Therefore, some things on cplusplus.com, for example, might not work on the Arduino, but everything in AVR-libc will!
  7. AVR Libc FAQ
    1. For ex: see the section titled "Can I use C++ on the AVR?", among many others...
  8. Learn to write macros, for example, in place of very short functions or repeated operations. Macros are very useful, and you should probably begin to learn to understand and use them as an intermediate programmer. See the top of the "Expert" section below for more information and links. Many of the Arduino "functions," as shown on the Arduino Reference page are actually precompiler operations, in the form of macros in this case, rather than true functions.  Macros are basically just textual substitutions in your code, and they get substituted into your code by the precompiler *before* the compiler interprets and converts your C code.
3-Advanced:
  1. By Ken Shirriff:
    1. http://www.righto.com/2009/07/secrets-of-arduino-pwm.html - an absolutely FANTASTIC article on the intricacies of controlling the low-level PWM capabilities of an AVR microcontroller on an Arduino
  2. By Nick Gammon:
    1. http://gammon.com.au/interrupts - an IMMENSELY USEFUL and well-written article, by Nick Gammon, on AVR microcontroller interrupts
    2. http://www.gammon.com.au/forum/?id=11504 - another EXTREMELY USEFUL article by Nick Gammon, on AVR microcontroller timers and counters
    3. http://www.gammon.com.au/forum/?id=11497 - Power saving techniques for microprocessors - very useful if you want your Arduino project to run for a very long time, on battery power, utilizing sleep modes, without a voltage regulator, etc etc.
  3. http://playground.arduino.cc/Main/TimerPWMCheatsheet - excellent info. to help you modify your Arduino PWM output frequencies quickly
  4. ATmega328 Datasheet:
    1. http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf - this is the 660 pg. datasheet of the Atmel ATmega 328 microcontroller, used by most Arduinos -- this is an absolutely ESSENTIAL document if you plan on doing low-level Arduino microcontroller programming, including direct manipulation of its timers, ports, registers and things
      1. Find the main ATmega 328 webpage here:  http://www.atmel.com/devices/atmega328p.aspx
  5. By Mikal Hart :
    1. http://arduiniana.org/ - Arduiniana - Arduino wisdom and gems by Mikal Hart
  6. http://yourduino.com/sunshop2/ and http://arduino-info.wikispaces.com/ - I have found a lot of good, useful info here.
4-Expert:
  1. Learn to use the C Preprocessor, and write Macros (writing macros is a very important skill, as many of the core Arduino "functions," such as lowByte(), highByte(), bitWrite(), etc, are actually macros).
    1. The following macros are defined in "Arduino.h" (found on your hard drive at Arduino\hardware\arduino\avr\cores\arduino\Arduino.h), for example:
      1. #define lowByte(w) ((uint8_t) ((w) & 0xff))
      2. #define highByte(w) ((uint8_t) ((w) >> 8))
      3. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
      4. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
      5. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
      6. #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
    2. The C (& C++) Preprocessor (and Macros) tutorial
      1. http://www.cprogramming.com/tutorial/cpreprocessor.html
    3. C Preprocessor Directives
      1. http://www.cprogramming.com/reference/preprocessor/
  2. Arduino Core Library C/C++ Soure Code - Learn to answer your own questions about how Arduino works by reading its C/C++ Source Code for yourself!
    1. View it on your hard drive, in the Arduino installation directory.
    2. View it on GitHub here: https://github.com/arduino/Arduino - use the search box at the top to search the Arduino source code repository, which is very useful to find the definition of various macros or functions you are looking for, since it searches *inside* the files too, not just the file names!
  3. Compiler Optimization settings: learn to change the GCC compiler optimization settings to optimize your compiled C/C++-to-Assembly-code for compiled size vs run-time speed, etc.
    1. Instructables.com: Arduino IDE 1.6.x compiler optimisations = faster code, by Bodmer
    2. https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html 
    3. https://gcc.gnu.org/onlinedocs/gnat_ugn/Optimization-Levels.html
    4. http://www.rapidtables.com/code/linux/gcc/gcc-o.htm
  4. Use #pragma GCC optimize ("-O2"), for example, at the top of your code to change optimization level without having to edit the Arduino platform.txt file (which was described on the Instructable above) - https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gcc/Function-Specific-Option-Pragmas.html
  5. Learn how to use gcc Attributes (ex: "__attribute__ ((aligned (4)));", "__attribute__ ((__packed__)))", etc.
    1. Function Attributes
    2. Variable Attributes
    3. Type Attributes
    4. Attribute Syntax (more here)
      1. Key points:
        1. "You may optionally specify attribute names with ‘__’ preceding and following the name. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use the attribute name __noreturn__ instead of noreturn."
        2. "Type Attributes: An attribute specifier list may appear as part of a struct, union or enum specifier. It may go either immediately after the struct, union or enum keyword, or after the closing brace. The former syntax is preferred..."
  6. Structure Packing & Data Alignment
    1. http://stackoverflow.com/questions/30038172/adding-unused-elements-to-c-c-structure-speeds-up-and-slows-down-code-executio - links & general info; also info on Data Alignment in the answer by Collin Dauphinee.
    2. http://www.catb.org/esr/structure-packing/ - Structure Packing
    3. http://www.songho.ca/misc/alignment/dataalign.html - Data Alignment
    4. More on "__attribute__((__packed__))" [<--gcc only] and "#pragma pack(1)" [<--preferred]
      1. *****Q: What's the difference? http://stackoverflow.com/questions/32208805/what-is-the-difference-between-attribute-packed-and-pragma-pack1
        1. A: "__attribute__((__packed__))" is for gcc (GNU C++) compiler only, whereas "#pragma pack(1)" was originally a Microsoft Visual C++ directive, and is now available in gcc and most compilers. Also: the former applies to the definition it is attached to only; the latter (#pragma pack(1)) applies from that point on, until undone by calling #pragma pack() with no arguments.
      2. *****+https://gcc.gnu.org/onlinedocs/gcc/Structure-Packing-Pragmas.html. Key points: 
        1. #pragma pack(n) simply sets the new alignment [to n, where n is a power of 2, ex: 1, 2, 4, 8, 16]; see the Microsoft link below as well
        2. #pragma pack() sets the alignment to the one that was in effect when compilation started
      3. *****Microsoft's description of "pack". Key points: "pack takes effect at the first struct, union, or class declaration after the pragma is seen. pack has no effect on definitions. Calling pack with no arguments sets n to the value set in the compiler option."
      4. ****Google search for "pragma pack"
      5. http://stackoverflow.com/questions/14179748/whats-the-difference-between-pragma-pack-and-attribute-aligned
      6. https://gcc.gnu.org/ml/gcc/2015-06/msg00112.html
      7. http://stackoverflow.com/questions/8568432/is-gccs-attribute-packed-pragma-pack-unsafe
  7. Writing efficient C code for an 8-bit Atmel AVR Microcontroller
    1. AVR035 Efficient C Coding for AVR - doc1497
    2. AVR4027 Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers - doc8453
  8. Viewing Arduino C/C++ "disassembled" (Assembly language) code
    1. http://rcarduino.blogspot.com/2012/09/how-to-view-arduino-assembly.html
    2. http://stackoverflow.com/questions/919781/how-can-i-get-an-assembly-language-listing-of-my-arduino-sketches-on-windows
5-Professional:
  1. Todo: add links here; ex: how to produce a professional product perhaps...

A Few Great Arduino Libraries Worth Mentioning:

  1. Arduino Playground: 
    1. Libraries: (Main Arduino user-contributed library list--this link copied from above under the "Intermediate" links): http://playground.arduino.cc/Main/LibraryList
    2. Snippets And Sketches for Arduinohttp://playground.arduino.cc/Main/SketchList 
  2. By Me: click on my "Table of Contents" link at the top of my website, then look under the "ARDUINO LIBRARIES" heading for a list.
  3. By Ken Shirriff:
    1. Arduino-IRremote - http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html
  4. By Mikal Hart:
    1. http://arduiniana.org/ - see his libraries list in the right-hand pane
  5. By Duane B:
    1. http://rcarduino.blogspot.com/2012/10/arduino-serial-servos-20-servos-4-pins.html - he has some great servo code snippets and libraries for the advanced user. For example: control 20 servos using only 4 pins and 2 x 4017 dirt-cheap decade counter IC chips.
      1. Base code on his website: http://rcarduino.blogspot.com/2012/08/arduino-serial-servos.html 
  6. Also look for anything by Adafruit/LadyAda (Limor Fried) and Simon Monk, of course.
    1. Adafruit provides code and libraries for nearly every single product they sell. They are one of the #1 supporters of Arduino in the world, and they are US-based!
  7. Sparkfun can be a good resource too--also US-based!

Other Inspiring Links:

The End.

If you like this article, please be sure to subscribe to my blog, tweet and like it on Facebook!  Links to like and subscribe can be found just below, and at the upper-right of my blog.  Look for the "Subscribe & Share" section to the top-right!  Also, feel free to leave comments below.





***Subscribe by Email!***

2 comments:

  1. Hi can you share your throttle hack code?

    ReplyDelete
    Replies
    1. yes sir. Source code link added to article. Source code on github. GPL V3.0 license.

      Delete

Thanks for your comment or question! If it is a question, I will try to get back to you quickly. Notice to spammers: I personally remove all spam promptly and report spammers to Google, so please don't do it.

Note: some HTML tags are allowed in your comments. To learn how to add bold (<b>...</b>), italics (<i>...</i>), or hyperlinks (<a href="URL">NAME</a>) to your comments, read here.

~Sincerely, Gabriel Staples.

P.S. Yo hablo español también. Je parle français aussi. (I speak Spanish & French too).