Creating Arduino Projects Using Sample Code


This page is a spinoff project from the Greyhound Sensor Rig project. I started working on that project in July of 2016, and as I begin this page, on October 29th of 2017, it’s not quite done yet. A lot of the learning that I had to do to complete that project was simply figuring out how to put the code together. The thing is, you don’t have to know how to write code from scratch to build an Arduino project using DIY components from reputable vendors (AdafruitSparkFunSeeedBoldport). If you understand the basics of working with Arduino (ie, how to import a library), and you understand more or less how the code works, you don’t need to write it all yourself. Lots of other people have written open source sample code for these components that you can integrate to make a fully functional project. The question for me was, HOW? So, if you’ve done a few Codecademy tutorials, and you’ve already read some of the Getting Started With Arduino materials, and now you want to like, make a clock to take to your school and show your teachers, this is how I would suggest you go about doing that…

  • Start by looking online to see if someone has already made the project that you want to make, and if they have already posted a how-to guide. There are lots of these out there. If this exists, great! You’re pretty much done. Or at least, you probably don’t need me anymore.
  • If there isn’t one, I suggest that you start by envisioning your project, and make a list of components that you think you’re likely to need. Simple Google searches can help you determine this. If you’re having trouble coming up with a project, I’d start by perusing the websites of the companies that make plug and play DIY components to see if inspiration strikes.
    • Note: You can get really cheap components from China and elsewhere fairly easily. Some of these will work fine! Otherwise will work less well. When you are sourcing them, you want to check and see what kind of documentation you can find for them before you order them. You can read about my Real Time Clock (RTC) chip horror story here, but TL;DR: you want to make sure that the component you order works. Read reviews, make sure there’s a data sheet or a wiki page that exists for it, check to see if someone’s written a library for it that you can find and download, see what kind of sample code exists already.
  • Pick an Arduino. There are a few models, so make sure that you get one that can handle the project that you want it to run. The current standard is the Arduino Uno Rev 3. It will run you about $30 CAD. If you need a step up from that (ie, more pins), you’d be looking for the Arduino Mega.
  • Before ordering your components and your Arduino, make sure that they will all fit together. You may have some components that are analog, some that are digital, and some that use I2C to connect to the Arduino. Make sure that you have enough pins of each type to connect the components to the Arduino. If you don’t, pick a bigger Arduino, or pick a component that uses a different method, OR find a shield that you can use to increase the capacity of your Arduino.
  • Order the things!

You have all your components, your Arduino, a whole bunch of documentation, and a project in mind. Now what?

  • 1: Find sample code
    • First, find the best sample code that someone’s written for your component. Sometimes, there are multiple samples out there. Adafruit generally makes the highest quality sample code, so I would start with theirs.
    • It may be easier to find sample code using the name of the chip that’s in the component, rather than the actual component itself. For example, the Grove barometer sensor contains the chip BMP280.
  • 2: Libraries
    • Next, you want to check if you need to add any libraries to your Arduino application and code to use the components you have. Install any necessary libraries. If you need some guidance, check the Arduino Guide to Libraries.
    • Make sure that at the very beginning of your sketch, there’s a list of all the libraries that you intended to include. The code should look like: #include <thenameofyourlibrary>
    • In my sketches, I will add a comment at the very beginning as a heading just to indicate that these are my libraries (//LIBRARIES), and then I include comments to indicate which component the library is for.
  • 3: Integrate sample code
    • Open your sample code. This may be downloaded as an Arduino sketch, or it may be online.
    • Test the code with the component that it is used for, to make sure nothing in the individual component or the sample you’ve chosen is broken. Troubleshoot as needed.
      • Make sure to adjust the code so that it reflects reality. For example, the sample code may indicate that the component you’re using is connected to digital pin 2, when in fact you’ve connected your component to pin 5. If you’re using multiple components, you can organize these as you wish, as long as the code is adjusted accordingly.
      • Remember, for components that connect over I2C, you don’t have to define the pins that they use.
    • If you are working on your first component, simply copy and paste the code into the sketch you will use for your completed project, and test it in the new sketch just like you did in the test sketch, and you’re done.
    • For subsequent components, you want to look at what sections there are in your code. You want to maintain the order of operations in the integrated code, not the unitary nature of the code itself. Break it into pieces, but make sure things stay in order. Make sure to include comments that indicate which components the lines of code are for. The following is my recommended order, and how to recognize each part of the code. Note that this is beginner level, and isn’t intended to be all encompassing.
      • libraries (#include)
        • If any of your components use I2C to connect to the Arduino, you need to include the Wire library, by adding #include <Wire.h> – for some reason Arduino calls I2C “Wire”? I’m not sure why it doesn’t just have a feature where it automatically recognizes that you may use I2C to connect, but it doesn’t.
      • component definitions (preprocessor definitions, #define; and variable definitions) and instantiation
        • include anything that sits above the setup code that isn’t a library
      • setup
        • include void setup() once, and anything that comes between that and void loop() goes in between
        • when you’re integrating these sections, you must read the code for each component and assess to ensure that the setup code for one component doesn’t include something that will interfere with the setup code of another component
        • anything that includes pinMode should usually go first, since the other setup elements for the components will need to know whether the pin is an output or input pin in order to function
        • some sensors have configuration code that should also be included in the setup section
      • loop
        • include void loop() once, and anything that comes between that and the end of the loop section (look for the closing bracket!)
        • a note on delay: most loop sections will include a delay somewhere in them (it will look like: delay(a number);). This is to control the speed of the microprocessor, which is often fast enough that it will feed you a large amount of data. Too much data. Unless it is serving another purpose, go through your loop section, and remove the arbitrary delays. At the end of your loop section, include a single delay of an amount of time that makes sense. The amount that makes sense will depend on the project, but you can try delay(1000); to start – that’s a one second delay – and adjust from there. Include this right before the closing bracket for your loop.
      • subroutine
        • in some code, there may be subroutine called by the setup or loop that sit sort of randomly around your sketch, although usually at the end. Include these after the loop.
  • 4: Verify, upload, and test! Followed by troubleshooting. Always, troubleshooting.
    • In your troubleshooting, if you’re getting thrown random errors that don’t seem to have anything to do with what you just edited, make sure to double-check the sections you did edit to make sure there aren’t any extra open or close brackets hanging around.
  • 5: Clean your code.
    • when you’re integrating example code, you can basically remove anything that feeds data to the serial port. This data will essentially be lost in the process, so you’re just asking your hardware to do extra work, and then toss it out.
Print Friendly, PDF & Email