CR201 Tutorial – Class 2

Welcome to the second tutorial for the CR201 course. This tutorial introduces buzzers and DC Motors, and also reintroduces the rover.

The Buzzer: Making some Noise

A CAROBOT SwissCHEESE buzzer.

This device here is a buzzer – a component that makes sound. By changing the frequency of the buzzer, we can adjust the pitch of the sound it creates.

The definition of frequency is the rate of a repetitive action. For sound, this is important since sound is the vibrations of the molecules in the air. So, faster the molecules vibrate, the faster the sound.

The chart below tells you the different frequencies required to make the buzzer play different notes. The bolded row in the middle is for the octave of middle C.

 

OctaveNote
 CC#/DbDD#/EbEFF#/GbGG#/AbAA#/BbB
3131138146155164174184195207220233246
4261277293311329349369391415440466493
5523554587622659698739783830880932987

To tell the buzzer to create sound, we need to use the function called “tone”: tone(buzzer_pin, frequency, duration);

If we want to stop the sound, we need to use the function called “noTone”: noTone(buzzer_pin);

Exercise 6: Using the Buzzer

Connect a button to pin I0 on your board, and plug a buzzer into pin O0. Then, write the following code into your program:

#include <CAROBOT_SwissCHEESE.h>

int NOTE_D4 = 293;

int NOTE_G4 = 391;

int NOTE_D5 = 587;

 

void setup() {
  pinMode(O0, OUTPUT);
  pinMode(I0, INPUT);
}

void loop() {

if (digitalRead(I0) == HIGH) {

tone(O0, NOTE_D4, 200);
delay(200 + 50);
noTone(O0);

tone(O0, NOTE_D4, 200);
delay(200 + 50);
noTone(O0);

tone(O0, NOTE_D4, 200);
delay(200 + 50);
noTone(O0);

tone(O0, NOTE_G4, 800);
delay(800 + 50);
noTone(O0);

tone(O0, NOTE_D5, 1000);
delay(1000 + 50);
noTone(O0);
}

}

If you are successful, your buzzer should play a short tune!

Try experimenting with more notes to create your own song. If you want, you could even try playing a song that already exists.

CHALLENGE: Combine this with the LED(s) to create a music/light show or music box with lights!

The Truth Behind analogWrite

This is mostly just for knowledge purposes, but in case you want to know about how the analogWrite function actually works, this is for you.

When you use the function analogWrite(), the Arduino actually does not output a true analog voltage. But, because the Arduino is so fast, it can fake it using something called PWM (“Pulse-Width Modulation”).

The Arduino is so fast that it can blink a pin on and off almost 1000 times per second. PWM goes one step further by varying the amount of time that the blinking pin spends HIGH vs. the time it spends LOW. If it spends most of its time HIGH, a LED connected to that pin will appear bright. If it spends most of its time LOW, the LED will look dim. Because the pin is blinking much faster than your eye can detect, the Arduino creates the illusion of a “true” analog output. Not only does it trick your eyes, it also tricks other electronics devices. What they experience is the average of the signal and not a true analog signal. This allows us to control the average voltage going into any electronic device.

DC Motors

The usefulness of a robot is very limited if it can’t move. There are a few ways for a robot to move but the simplest solution is with a DC motor.

As you may have learned already, a DC motor is a device that transfers electrical energy into motion, or mechanical energy. The output power of a motor is directly proportional to the product of the input voltage and the input current. The amount of current that can go into a motor is defined and usually limited by its design but the voltage is what we can control. By controlling the voltage, we can control the power output of a motor thus the speed.

We will be reusing the rover from the last classes. If you need help building it, refer to the previous tutorials.

Now, connect a DC motor to the M1 port on the SwissCHEESE board. In case you don’t remember how to plug in the motors, loosen the screws on the motor ports, insert the wires (black in the first M1 port, then red in the second one), and tighten the screws again. Then, connect a potentiometer to port I0. Once you’re done, input the following code into the IDE:

#include <CAROBOT_SwissCHEESE.h>

SCMotor motor1(M1);
int m1_speed;
void setup() {
pinMode(I0, INPUT);

}

void loop() {

m1_speed = analogRead(I0);
motor1.speed(m1_speed);

}

This code should allow you to control your motor speed using the potentiometer. The motor will read from the potentiometer’s analog input, matching its speed with the values it’s reading.

CHALLENGE: Try to control a second motor using the same potentiometer.

Motor Calibration

One of the major disadvantages of differential drive is the difference in motor speed or wheel diameter due to manufacturing imperfections. To handle this problem, we need to add a correction factor to the motor speed. For example, if you want your robot to go straight but the robot is turning towards one side, you’ll need to increase or decrease the speed of one of the motors.

Use the following code to and determine the speed for each motor in order for the robot to move in a straight line.

#include <CAROBOT_SwissCHEESE.h>

SCMotor motor1(M1);
SCMotor motor2(M2);

void setup() {

pinMode(I0, INPUT);

}

void loop() {

if (digitalRead(I0) == HIGH) {

motor1.speed(255);
motor2.speed(255);
delay(2000);
motor1.stop();
motor2.stop();

}

}

TASKS:

  1. Determine the motor speed in order for the robot to move in a straight line.
  2. Create two variables called m1_factor and m2_factor and use that to correct the difference in speed ie. motor1.speed(255 * m1_factor) and motor2.speed(255 * m2_factor). Note: you’ll need to determine the value of factor and assign it to the variable.

Bumper Rover Challenge

Create a rover that can navigate around obstacles using bumpers.

Mount two buttons to the front of your robot using links, nuts, and bolts as the bumper. Both buttons should be facing forward with one on the very left and one on the very right. Mount another button on the side of your robot to act as the control button.

Attach all three buttons to your SwissCHEESE shield and program the Bumper Robot.

Some example logic:

  • if the left button bumped an object, back-up and turn right
  • if the right button bumped an object, back-up and turn left
  • else move forward

It is highly unlikely that both buttons will be pressed at the same time, but you can consider that in your test case as well.

Consider using a nested if/else statement and a “flag” variable to control when your robot starts moving.

When the robot is moving as intended, start getting creative! Add some lights and sound to your robot to make it a bit fancier.

Once you’re done, you’ll be finished this tutorial.  Hope you look forward to the next one!