Arduino to AVR: Get Started in 3 Steps

Arduino to AVR: Get Started in 3 Steps

Arduino to AVR: Get Started in 3 Steps Beyond the Arduino: Gain Flexibility and Control by Working Directly With the AVR Microcontroller Andrew Retallack Copyright © 2015 Andrew Retallack Andrew Retallack asserts his moral right to be identified as the author of this work. All rights reserved. No part of this publication may be reproduced, or transmitted in any form or by any means, electronic or otherwise, without written permission from the author. Disclaimer While every effort has been made to ensure the correctness of the information in this work, the author assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. 2 www.crash-bang.com Why Make the Move from Arduino? You’re starting to become an expert on an Arduino board, and can get it to do your bidding without too much effort. Motors whir at your command, LEDs flash at a click of your fingers, and you’re hooked up to the Internet-of-Things and GPS-located - so why would you even consider a move away from this environment? I was in your shoes a few years ago, and in fact still had a good deal to learn in the Arduino-world; but then a little project came along that just wasn’t a fit for my Arduino UNO. In trying to find a better solution, I ended up spreading my wings and leaving the Arduino environment behind me - and I’ve loved every minute of it! Alright, perhaps not every minute of it - it was in fact a pretty bumpy road. The challenges and my hard-earned learning inspired me to establish my website, blog, and the “Beyond the Arduino” series of articles I write for Nuts and Volts magazine. Even though there are many challenges along the way, I passionately feel that there is much to be gained from moving from the Arduino environment to working with bare-metal microcontrollers. Here are some of the reasons that I’d encourage you to make the leap! Form Factor The dimensions and shape of my Arduino UNO were what first sent me looking at alternatives. At 55mm by 75mm, it was a chunky rectangle that didn’t always fit into the casing that I needed it to. The ATmega328P microcontroller on the UNO is, by comparison, only 35mm by 10mm in size, giving you the opportunity to squeeze your project size down substantially and explore the Why Make the Move from Arduino? creation of custom-shaped PCBs. You can get even smaller by using surface mount packages - the TQFP is only 9mm square. Flexibility Arduino boards are great if you only need to run at one speed and one voltage - of course I’m talking about the 8-bit AVR range, where you can choose between 3.3V & 8MHz or 5V & 16MHz options, but nothing in between. Flexibility becomes critical when looking at building low-power (often IoT) projects. To reduce your project’s consumption you may want to use a 32.768kHz watch crystal, or run at 2.4V powered by a couple of AAA batteries. Functionality Abstraction: The Arduino development environment makes life easier for users by abstracting a lot of the complexity. However, as you’d expect, this simplification comes at a cost - removing your access to some useful AVR functionality, for example: ● Timers: The ATmega328 has 3 timers - one of these is tied up controlling the PWM and the millis(), micros() and delay() functions. The other two are used for phase-correct PWM. This prevents you from using any of the timers for your own purposes, which in turn limits your ability to write optimized code and reduce your project’s power consumption. ● ADC: The ATmega’s ADC has a range of features that control the sample rate, the ways that readings are triggered, the use of interrupts and a way to reduce noise for higher precision readings. Unfortunately these are not accessible through the Arduino libraries. 4 www.crash-bang.com Why Make the Move from Arduino? ● Sleep Modes: Sleep mode is a critical tool in reducing power consumption and using your microcontroller in an optimised way. One of the ways to wake the microcontroller is using a timer - but unfortunately the timers aren’t available, leaving only a couple of non-time-based ways to wake the microcontroller from sleep. Speed: In addition to limiting the functionality, all the abstraction adds overhead. In a simple project running at a 16MHz clock speed this may not be important - but slow the clock down and include a few time-critical functions, and speed can become critical. For example, a simple digitalWrite() instruction to turn a pin on/off takes approximately 100 clock cycles (or 6μs) - more than 60 times slower than a direct call would be. An in-depth look at digitalWrite() can be found at a post on my website. Greater Choice Working directly with microcontrollers opens up a world of choices that the Arduino ecosystem doesn’t support. What if you only need an 8-pin microcontroller for your project - or a microcontroller with built-in USB support - or a 32-bit microcontroller? If you’re already working with the bare-metal AVR microcontrollers, these options are instantly accessible to you without much (if any) learning curve. The 8-bit AVR’s work off similar register sets, which allows you to choose from a range that includes 8 to 100 pins, USB controllers, LCD controllers, LIN and CAN controllers, a variety of FLASH and EEPROM memory sizes, and low-power options. This allows you to move on to design projects with highly optimised BOMs. Build Your Own Real Product One of the highlights of my time working on embedded systems was the day that my first custom PCB arrived in the mail - a purple PCB with beautiful gold pads that I had completely designed myself. In order to design this PCB, I needed to be comfortable Arduino to AVR: Get Started in 3 Steps 5 Why Make the Move from Arduino? moving away from the Arduino environment, and to have a solid understanding of how to fit a raw microcontroller into a standalone project. From that start I went on to design a number of boards for my own projects, as well as a few which featured in popular magazines and went on to sell well. I would never have experienced this satisfaction if I had restricted myself to the Arduino UNO and its various shields. Lower Cost While there are a number of low-cost Arduino-compatible boards available, they’re still likely to cost more than building your own board with the specific set of features that you need for your particular project - whether you build your project at scale on manufactured PCBs, singly on PCBs you’ve etched yourself, or even on a hand- soldered strip-board. The Arduino boards are great for prototyping on, but it probably doesn’t make sense to include one into each of your projects. The Challenge and Achievement All the reasons I’ve listed so far are tangible with specific benefits. But I bet that you also want to understand how things work behind the scenes, that you’re not afraid of a small challenge, and that you know what it feels like to have that sense of achievement when your project does what it’s supposed to do. I have to be honest and admit that, at the outset, I didn’t know about all the benefits I’ve mentioned so far - I started with microcontrollers out of curiosity and because I wanted to keep challenging myself. I couldn’t believe that I could actually build my own Arduino-on-a-breadboard, so I jumped in with both feet and went from there. If you’re the same, then welcome to the club - put your boots on and jump in too! 6 www.crash-bang.com Step 1: Choose Your Hardware Right, so you’ve decided to give this bare-metal microcontroller challenge a go - where to from here? First up you need to decide what microcontroller you want to use, and then how you’ll be using it. There are a staggering number of choices out there, and it can be tempting to spend time finding the perfect microcontroller and the perfect platform - my suggestion is that you read through this section, and then put a peg in the ground and get started. Which Microcontroller? Unless you’ve been living off the grid, you’ll be aware that there are more prototyping platforms out there than just the Arduino - many designed by the manufacturers themselves as a way to get users comfortable with their specific microcontrollers. You’ll likely have come across LaunchPad boards from Texas Instruments, the PICkit boards from Microchip, and a host of others from STM, Silicon Labs, and of course Atmel. While there are common elements and methodologies across the various manufacturer’s microcontroller families, there a large number of differences. It’s for this reason that most people tend to stick with the manufacturer they got started with. So which microcontroller should you get started with? My recommendation is to begin with where you left off; in other words, continue with the microcontroller on the Arduino UNO - Atmel’s ATmega328P. There are a number of reasons for my suggestion: Step 1: Choose Your Hardware ● Familiarity: You’re already comfortable with some of the features and functionality on the ATmega328P, even though they have been hidden behind the Arduino IDE. ● Friends: Large numbers of hobbyists and enthusiasts have gone this same route, and used the ATmega328P for their first “raw” microcontroller projects.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    25 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us