Arduino Projects with Motion (Using Sparkfun RedBoard)

Updated 01 June 2018

Arduino Projects with Motion (Using Sparkfun RedBoard)

Welcome to the Third Project Set from the Sparkfun Inventors Kit V4.0 Guide! In this tutorial, we will explore the operation of a Servo Motor and an UltraSonic Sensor in combination with some other components that we discovered in Project Set 1 and Project Set 2. By the end of this tutorial, we would have created a motion alarm which fuses all of our current Arduino and electronics knowledge into one nifty circuit! Let's begin!


Servos and Ultrasonic Sensors

A Servo Motor is a small little motor that can be controlled by Pulse Width Modulation (PWM; discussed in Project Sets 1 & 2). By sending a pulse-train (a PWM signal) to the Servo Input Pin (any of the PWM Pins; 3, 5, 6, 9, 10, 11) the Motor will rotate to a position based on the width of the pulses in the pulse-train. The Servo Motor included in the SIK Ver 4.0 is known as a 'fixed rotation servo' with its range of angular motion from 0° - 180°. With that said, we often don't want to push the Servo to its angular extremities as this can result in the Motor twitching so we will be limiting the angular range from 20° - 160°. We've also put together a guide about how Servo's work if you were interested to learn more.

The other new component we will be incorporating into our circuits is an UltraSonic Sensor. The sensor we will be using is the HCSR04 (datasheet). An Ultrasonic Sensor uses 'low frequency' (8 x 40kHz) sound waves to detect an object in its path. If we know the time that it takes for these signals to be sent and received and we also know that the speed of sound is approximately 343m/s at sea-level and 25°C, then we can work out the distance of the object from the sensor. The HCSR04 module, as outlined in the datasheet is accurate (within 3cm) from 2cm - 4m! It does, however, have a 15° 'cone' from the transducer (T and R) so it must be placed at the end of the table it's sitting on. We have a guide about how Ultrasonic sensors work if you'd like to explore them more.

sparkfun-servo-and-ultrasonic


Project 3 - Circuit 3A

This circuit will introduce 3 new programming concepts that we will expand on in future Project Sets and circuits. The first, new concept is the inclusion and use of libraries. A library is a set of functions made by a person that makes life easier when it comes to programming. This could be due to simplification of writing code (i.e. a math library that allows for more efficient writing of math; e.g. x = pow(2,7) as opposed to x = 2 * 2 * 2 * 2 * 2 * 2 * 2) or because the functions in the library are designed for a specific component. In this case, we will be including the Servo library to help with initialisation and writing a position to it.

Most libraries that relate to a component (Servo, LCD, LED matrix) will use an object and methods to tell the RedBoard what to do; this is the second, new concept. After including the library, an object must be created so that its methods can be accessed. This is shown below with an example of a method.

library-include-servo-one

method-example-servo-one

The final concept is related to a function. When we're operating with different degrees of magnitude (i.e. 8-bit, 10- bit, 0° - 180°) scaling values becomes quite important. To do this, we use the map() function. It takes 5 arguments (inputs) that are comma separated. A thorough description of the function can be found in the Arduino Reference and we explore it further in the Circuit 3A Video.

Important notes:

  • Disconnect the RedBoard from the computer before connecting anything to the Power Bus
  • Ensure +5V isn’t connected straight to GND through the power rail or potentiometer
  • Make sure the map() function is included and that it's new extremities aren't too close to 0° or 180°

Project 3 - Circuit 3B

This circuit will introduce the HCSR04 Ultrasonic Sensor to our electronics toolkit. Unlike the Servo, the HCSR04 doesn't require a library to be included manually, nor does it use objects or methods. We will use a function to calculate the distance in this sketch such that our loop function isn't too long or complicated (it's also good practice to modulate any simple, repetitive tasks). The HCSR04 has 4 Pins:

  • VCC (power)
  • Trigger 
  • Echo
  • GND

The VCC and GND Pins we have met before however, the two Middle Pins are new to us. The Trigger Pin is an output configured to send 8, 40kHz signals when set high for at least 10µs. Initially, we will set the Pin low to clear it, then high for a 10µs period and then low again and that is its job done. The Echo Pin is an input that will be used to capture the time it takes to register the reflected 40kHz signals from the detected object. This time will be used along with the speed of sound to calculate the object's distance as stated above. This Circuit also uses a new programming feature when declaring the Pin assignments at the start of the program. 'const int' is used over 'int' for the variable type; this makes the variable a "read-only" variable, known as a constant.

Important notes:

  • Disconnect the RedBoard from the computer before connecting anything to the Power Bus
  • Ensure +5V isn’t connected straight to GND through the power rail or otherwise
  • Make sure that the RGB LED is connected the right way

Project 3 - Circuit 3C

Once again, we have reached the final Circuit in a Project Set and, once again, it utilises all that which came before it. This Circuit has a Servo, an Ultrasonic Sensor, a Buzzer and an RGB LED! If you would like, you can print out a small photo of an animal to pin on the Servo via a paperclip. I've instead chosen to draw a creature to help complete this terrifying alarm system! This Circuit is an amalgamation of the previous 2 Circuits with an added Buzzer, hence, assembly should be completed the same way.

Important notes:

  • Disconnect the RedBoard from the computer before connecting anything to the Power Bus
  • Ensure +5V isn’t connected straight to GND through the power rail or otherwise
  • Make sure that the RGB LED and Buzzer are connected the correct way with respect to polarity and current limiting resistors

Congratulations! You now have the skills to keep a sibling or pet out of your room when you're not around using your Sparkfun RedBoard! Project Set 4 is waiting for you to take it on! It covers the use of an LCD in an Arduino environment to display measured data or text. If you'd like to look at RedBoard projects involving Sound, check out Project Set 2.   

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.