Other Features of Sparki

Session 5

Nanica.io
Teaching Sparki
8 min readSep 18, 2015

--

We will be looking at Sparki’s accelerometer, magnetometer, RGB LED, piezo buzzer, and LCD display. We will also be learning how to use bluetooth to get Sparki send and receive messages to and from the computer. We will be making music and simple animations. We will also learn a little bit about the Processing programming language which we will use to create the user interface in the computer for interacting with Sparki.

This session (number 5) can be completed within one or two hours.

THE BLUETOOTH MODULE

You can communicate with Sparki wirelessly (via Serial commands) using its bluetooth module. The bluetooth module should be inserted into Sparki’s the correct expansion pins. The bluetooth module has four communication pin: TX, RX, GND, and VCC.

The VCC and GND respectively provides the source of electricity and the ground path for the electricity to travel back from Sparki. Sparki uses the TX pin to transmit information out of Sparki and to the bluetooth module. Sparki uses the RX pin to receive information from the bluetooth module. The TX of the bluetooth plugs into the RX of Sparki. The RX of the bluetooth plugs into the TX of Sparki.

If you want to dig deeper, Sparki’s website has a lesson on this. But for a quick dive, please see the sample code below. In the code below, we use serial1 to receive commands from the computer where Sparki’s “eyes” should face: left, front or right. Sparki then performs a ping() and reports it back to the computer.

BLUETOOTH TEST CODE FOR SPARKI

The problem with Sparki’s bluetooth is it’s not very reliable. Sometimes it makes Sparki crash and reset. So we have adapted this code to go over USB too. Just replace serial1 with serial.

USB SERIAL COMMUNICATION TEST FOR SPARKI

We will be using this serial communication to send and receive message to and from Sparki with the computer. We will do this to get data from Sparki’s two sensors (magnetometer and accelerometer) .

We will be using the Processing language to make a simple user interface in the computer. You can learn more about connecting the Arduino to a Processing application from this really awesome tutorial from Sparkfun. Jeremy Blum also has a video explaining how he made a temperature display with Arduino and Processing.

Firstly, let’s understand how these two sensors work. We will then communicate these numbers via serial (USB or Bluetooth) to the commuter. We will read this and display it in a simple user interface with Processing.

THE ACCELEROMETER

Sparki’s 3-axis accelerometer detects the acceleration Sparki is experiencing in all 3 directions (x, y, z axis). It is the same thing that smartphones use to know how you’re tilting them.

Acceleration is how fast something speeds up which is measured as change in velocity over time. Gravity is also a force that causes acceleration. Objects will fall faster and faster will acceleration. Gravity is the only force with acceleration that Sparki measures when sitting flat on a surface.

This accelerometer is a microelectromechanical (MEMs) device. They are made of tiny mechanical parts like springs that are too small to see. When the part is accelerated this springs compress and relax. By measuring this, the part is able to measure the acceleration it’s experiencing.

The commands below return what the accelerometer reads in meters per second squared in each direction (in the form of a float).

PROGRAMMING EXERCISE: Display accelerometer readings over the serial monitor (USB or Bluetooth). Place Sparki right side up, what are the readings? Place Sparki up side down, what are the readings? Do these readings make sense?

Here is the code which we make an accelerometer display with processing. Sparki communicates the values via a USB serial port to the processing app.

CODE FOR SPARKI (communicating accelerometer values via Serial)

CODE FOR PROCESSING (receiving values via Serial + display)

THE MAGNETOMETER

Sparki also has a 3-axis magnetometer. This is used to detect the magnetic field Sparki is experiencing. This can mean the earth’s magnetic field, the magnetic field generated by Sparki’s motors and wires, among other sources. This is the same part that smartphones use to act as a compass. Like the accelerometer, it can do this in all 3-axis and it is also a MEMs device.

There are tiny coils and beams that are too tiny to see. Each axis has a beam which has a pressure sensor on each side and a coil wrapped around it passing current though it. Due to the Lorentz force, the stronger the magnetic field, the more the bar with the current passing though it bends. The pressure sensors measure this bending and Sparki can deduce from this the magnitude of the magnetic field is experienced.

The commands below return what the magnitude reads in meters milligauss in each direction (in the form of a float).

PROGRAMMING EXERCISE: Display magnetometer readings over the serial monitor (USB or Bluetooth). Place Sparki in different directions, what are the readings? Do these readings make sense?

PROGRAMMING EXERCISE: Can you adapt our code from our accelerometer display project above to show both magnetometer and accelerometer readings?

PIEZO ELEMENT

Sparki has a buzzer that can play various tones. Sending a voltage causes the buzzer to bend and flex. This moves the air around and creates sound waves which we can hear. The closer the waves, the higher note the sound is. We can change the frequency which the buzzer oscillates to make music.

You can use the command sparki.beep(tone); for this where tone is is a pitch value of the note. The pitch values of typical notes have been defined for you as taken from this tutorial from Arduino. We have adapted thiscode from princetronics to play the Super Mario songs on Sparki.

CODE TO PLAY SUPER MARIO SONGS ON SPARKI

PROGRAMMING EXERCISE: CAN YOU PLAY ONE OF YOUR FAVORITE SONGS ON SPARKI?

RGB LED

Sparki features an RGB (Red, Green, Blue) LED (Light Emitting Diode). An RGB LED combines three different colors of different intensities to create a broad range of visible colors. There are three individual LEDs inside the RGB LED.

Sparki sends a PWM signal (Pulse Width Modulation signal) which means Sparki is turning each LED on and off at an extremely fast speed. Our eyes (and even a multimeter) can’t keep up that they average the on and off values into something in between the minimum and maximum values. Check the graphs bel0w.

What matters when mixing colors is the ratio of the colors you mix. For example the code below all create the same color purple but with different intensities.

While the next commands create different shades of purple increasing their likeness to blue as you go down the list (the ratio of blue to red increases as you go down the list.

PROGRAMMING EXERCISE: CAN YOU MAKE SPARKI DISPLAY THE COLORS OF THE RAINBOW?

Code for Testing RGB

LCD DISPLAY

We can use Sparki’s LCD display to write text, make drawings and animations. All displays are composed of pixels which is the smalls points we can control in a display. In Sparki’s particular LCD, pixels can be one of two states: ON or OFF. Each pixel is on a particular point on the display which we can locate using the cartesian coordinate system. Sparki’s display is 128 pixels by 64 pixels.

Here are some high level commands you can use to print text or draw things on Sparki.

Let’s try making some animations with Sparki. tobiasahlin.com/spinkitlooks like a nice place where we can get inspiration on some simple animations. Animation 3, 6, 7, and 8 looks like fun!

here are some simple animations we have coded up for you

SUMMARY

Wow! You’ve made it! We’ve tackled Serial communication with bluetooth module (wireless), and USB port (wired). We’ve also discussed the magnetometer, accelerometer, piezo buzzer, RGB LED, and the LCD display. We made Sparki sing some music and display animations.

In the next and final session, we will be discussing what else you can do with the knowledge you’ve gained in this workshop. :)

--

--

Nanica.io
Teaching Sparki

The best and most fun way to learn and test your programming skills is with robots!