Reader small image

You're reading from  Arduino BLINK Blueprints

Product typeBook
Published inMay 2016
PublisherPackt
ISBN-139781785284182
Edition1st Edition
Tools
Right arrow
Author (1)
Utsav Shah
Utsav Shah
author image
Utsav Shah

Utsav Shah is an instrumentation engineer who loves to work on the latest hardware as well as software technologies. He has been featured on India's leading website http://yourstory.in and Ahmedabad Mirror (Times Group) for his research work on "Converting sign language into speech" using a Leap Motion controller. Apart from his regular work at Infosys Limited, he manages activities of Infosys Robotics Club. In his leisure time, he loves to read books and work on cutting-edge technologies.
Read more about Utsav Shah

Right arrow

Chapter 6. Persistence of Vision

So far in this book, we have made all things that are stationary in nature—that is, they can't move. In the final project of this book, we will create an even more intensive experience by moving LEDs using motors. We will create a Persistence of Vision wand using an LED array and motor. But, first of all, you will get introduced to LED arrays and motors. Along with the different type of motors, you will get to know about their pros and cons. In this chapter, we will cover the following topics:

  • Persistence of Vision

  • Programming an LED array

  • Controlling a motor using Arduino

  • Synchronizing LED array timing based on the speed of the motor

Creating your own Persistence of Vision


One of the five sensory organs of our body, the eye is a remarkable instrument that helps us to process light in such a way that our mind can create meaning from it. Persistence of Vision refers to an optical illusion where multiple different images blend into a single image in the human mind.

The Persistence of Vision illusion plays a role in keeping the world from going pitch black every time we blink our eyes. Whenever a retina is hit by light, it keeps an impression of the light for about a tenth of a second after the light source is removed. Due to this, the eye can't distinguish between changes that occur faster than this retention period. This similar phenomenon is used in motion pictures or, as we call it, "flicks". The motion picture creates an illusion by rapidly sequencing individual photographs. Usually for motion pictures, the rate of frames per second is 24, which leads to a flicker-free picture. If frames per second is kept below 16,...

Programming an LED array


An LED array is nothing but a few LEDs connected together. Usually, an LED array comes in sizes of eight LEDs and 16 LEDs. You can control an LED array directly using the digitalWrite() function. Apart from using the digitalWrite() function, you can control LEDs directly using port-level communication. On Arduino, we have three ports: ports B, C, and D:

  • Port B: Digital pins 8 to 13

  • Port C: Analog pins

  • Port D: Digital pins 0 to 7

Each port is controlled by three DDR registers. These registers are defined variables in Arduino as DDRB, DDRC, and DDRD. Using these variables, we can make the pins either as input or output in the setup function.

You can use the following syntax to initialize the pins:

DDRy = Bxxxxxxxx

Here, y is the name of the port (B/C/D) and x is the value of the pin that determines if the pin is input or output. We will use 0 for input and 1 for output. LSB (least significant byte) is the lowest pin number for that register.

To control pins using this port...

Different types of motors


Depending upon your project needs, you can choose from the variety of motors available in the market. For hobby electronics, you will mostly use either DC motor, servo motor, or stepper motor. The following image shows all three types of motors. From left to right, DC motor, servo motor, and stepper motor:

Let's get an overview about all the different types of motors.

DC motors

DC (Direct Current) motors are two-wire continuous rotational motors. When power is supplied to the motor, it will start running and will stop once power is removed. Most DC motors run at high speed, that is, high RPM (rotations per minute). The speed of the DC motor is controlled using the PWM technique (as discussed in Chapter 2, Project 1 – LED Night Lamp). The duty cycle will determine the speed of the motor. The motor seems to be continuously running as each pulse is very rapid.

Servo motors

Servo motors are self-contained electric devices that rotate or push parts of a machine with great...

Controlling a DC motor using Arduino


In this chapter, we will get to know how to control a DC motor with Arduino.

You can also run the DC motor by using the same code as the LED blink. We can consider the motor as an LED. As discussed earlier, a DC motor is a two-wired motor. One wire is the positive supply and other is ground. If we connect the positive terminal of the motor to the positive terminal of the battery, and the negative terminal of the motor to the negative terminal of the battery, the motor will run in one direction, and if we reverse the connection, the motor will run in the reverse direction.

By connecting the motor to two digital pins of the Arduino, we can control the direction of the motor. In the following basic code, we will run the motor in one direction for five seconds and then we will reverse the direction of the motor. Connect pin 3 and pin 4 of the Arduino with the two wires of the motor:

int motorPos = 3;
int motorNeg = 4;

void setup() {
  pinMode(motorPos, OUTPUT...

Synchronizing an LED array with a motor


In the previous sections of this chapter, we learned about controlling an LED array and DC motor using Arduino:

Once you have connected the circuit as shown in the preceding image, upload the following code to Arduino. In the following code, we are writing the "Hello world" of persistence of vision:

int LEDPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
int noOfLEDs = 8;

//data corresponding to the each alphabet and a few characters to be displayed
byte H[] = {B11111111, B11111111, B00011000, B00011000, B00011000, B00011000, B11111111, B11111111};
byte E[] = {B00000000, B11111111, B11011011, B11011011, B11011011, B11011011, B11000011, B11000011};
byte L[] = {B00000000, B11111111, B11111111, B00000011, B00000011, B00000011, B00000011, B00000011};
byte O[] = {B00000000, B11111111, B11111111, B11000011, B11000011, B11000011, B11111111, B11111111};
byte fullstop[] = {B00000000, B00000000, B00000000, B00000011, B00000011, B00000000, B00000000, B00000000};
byte comma...

Bringing your efforts to life


Once you know how to control motors and LEDs using Arduino, the final step is to put everything that you have learned so far and make it a standalone product. There are multiple ways you can achieve this:

  • The simplest method is to use your hands.

  • Using two different Arduinos or using external motors

  • Using existing real-life devices

Using your hands for rotation

Even though you learned about controlling motors and LEDs, if you are doing this for the first time, you will take some time to understand the synchronization of LEDs and motors. The easiest way to test Persistence of Vision is to use your hands for rotation. Before you start rotating the complete setup, make sure you have uploaded the latest sketch to Arduino, connected some standalone battery/power source, and firmly fixed your LEDs on some base. As a base, you can use a thermocol sheet or cardboard, and fix them using some electrical tape.

Once you have fixed all the materials, you can rotate your PoV, and...

Summary


In this final project of this book, you learned about motors and how to control an LED array. After understanding Persistence of Vision, we developed a Persistence of Vision display. After developing all the projects in this book, you should be able to play with different types of LED and make some innovative thing out of them. In the last chapter, we will learn about some of the problems that you might face while developing the projects explained in this book.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Arduino BLINK Blueprints
Published in: May 2016Publisher: PacktISBN-13: 9781785284182
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Utsav Shah

Utsav Shah is an instrumentation engineer who loves to work on the latest hardware as well as software technologies. He has been featured on India's leading website http://yourstory.in and Ahmedabad Mirror (Times Group) for his research work on "Converting sign language into speech" using a Leap Motion controller. Apart from his regular work at Infosys Limited, he manages activities of Infosys Robotics Club. In his leisure time, he loves to read books and work on cutting-edge technologies.
Read more about Utsav Shah