Lego EV3 Robotics, Math and Music

Group Size 3-4

Grade Level 8-11

This lesson is an exploration into the programming and mathematical world of music. Students will learn about the mathematics relationship between pitch (note), frequency and period, as well as the way of calculation of fret spacing for stringed instruments. Students will then apply their knowledge by constructing and programming Lego EV3 based musical instruments for playing music.

Preparing For This Tutorial:

Materials

  • Lego EV3 kit, 1 kit per each group

  • Computers with Audacity program installed. This is a free program available at: http://audacity.sourceforge.net/

  • Relevant worksheets: The Periods and Frequencies of Sound, Periods and Frequencies Practice, Guitar Fret Spacing, Lego Robotics and Music Project, Compose Music with a Lego

LEGO MODELS:

  1. Build a Lego guitar following the instructions here (this model is made by Daniele Benedettelli).

  2. Build Your Lego Music Sequencer. The music sequencer is a really simple build. You’ll construct a base with a motor to rotate an arm, and mount at the arm the EV3 Color sensor where it can detect the color of Lego brick. Then you’ll hook up the EV3 brick for the brain. Just follow the following instructions of Damien Kee model.

https://www.damienkee.com/storage/EV3_Sequencer_build.pdf

  • image0

Time constraints: 3-4 hrs

Exercises

Exercises are contained in the worksheets (some of them you will receive from the teacher during classes):

  1. Introduction to Sound and Audacity

  2. The Periods and Frequencies of Sound.

  3. Periods and Frequencies Practice.

  4. Guitar Fret Spacing.

  5. Lego Robotics and Music Project.

  6. Compose Music with a Lego Sound Sequencer Project.

Name: ____Date:____

Introduction to Sound and Audacity

  1. Record a steady tone on Audacity. You can use your voice to do this - make sure you keep the pitch steady and record the sound for long enough.

  2. Zoom in on the recorded sound so that you can see the sound wave.

  3. What does the x-axis represent?

  4. What does the y-axis represent?

  5. What do you notice about the shape of the graph?

(See Appendix A for how to use Audacity)

Name: ____Date:____

Lego Robotics and Music Instrument - Project

Practice

This is a project that is more about process than product. You are responsible for documenting your trials and errors in making your Lego instruments.

The Project:

You and your partner are responsible for creating and programming in Python a Lego EV3 based piano and guitar that produce at least five different pitches.

  1. Load the EV3Dev Operating System on each Lego smart brick with the readymade EV3Dev SD card, which is given to you by the teacher.

  2. Set the communication between the Lego brick and the computer: It is not mandatory to set up a network between PC and Lego but it will help to upload the program. Set up the network connection between the EV3 and the brick, as described here. Set up a graphical SSH connection between the EV3 and the PC, as described here.

  3. Turn EV3 smart brick into a small piano with 5 notes so that it can play different notes, as shown in this video. Necessary Python commands can be found below in Appendix D. Your code must be put on Google Docs and shared with your teacher.

  4. Make a Python music program for playing the “Happy Birthday” song with a Lego EV3 smart brick. Instructions to follow are given in Appendix B. Your code must be put on Google Docs and shared with your teacher.

  5. Build a Lego guitar following the instructions here (this model is made by Daniele Benedettelli). Of course you can use your imagination for building another model.

  6. Read and try to understand the meaning of the readymade Python program given in Appendix C. Write your thoughts about the functionality of the program on Google Drive and share them with your teachers. Test the program to verify your thoughts. For help, look at the Appendix D, in which you may find a list of the most useful Python instructions.

  7. Make the following changes that will make your guitar to look and function more realistically.

  • Watch this interactive video (create a student account in Edpuzzle platform with your details and join PROBOT class with code that your teacher will give you).The neck of the guitar is divided into 7 frets (for the 8 notes of octave). But actually, as you watched in video, the neck should be divided into 13 frets because there are also the sharp (#) notes. So, make the appropriate changes for that. [tip: 2 changes are needed: add the rest notes and add more elif statements].

  • In the readymade code, each note is given a specific tone (Hz). But (according to the video above) each note has the frequency of the previous one multiplied by \(\sqrt[12]{2}\). Instead of giving just a number for every frequency, use this information.

  • Give some visual effects at the guitar. The smart brick has 2 LEDs which can have 5 different colors. Select a random color for each note.

  • If you are not very familiar with guitar, you may need to know where each note is on the arm of the guitar. So, change the Lego beams of the upper row of the arm with colorful ones. Use a different color for each note if this is possible.

  • You may have noticed that there is also a medium motor plugged in guitar which is not used. Try to think of a way to make it useful.

All of your answers must be put on Google Drive and shared with your teachers.

  1. You may also check the tuning of your guitar by going to http://onlinetonegenerator.com/ and plugging in the frequency you believe your guitar is producing.

    Name: ____Date:____

Compose Music with a Lego Sound Sequencer Project

In this project you’ll make a simple step sequencer that uses colored Lego bricks to put sounds into a sequence that makes up a little song. You’ll use only a single EV3 Color sensor, but that sensor can detect 8 different colors.

Lego colored blocks are placed at the end of the sequencer arm, and as each block passes the color sensor, the sound that corresponds with that color is played. The variety of sounds and sequences is endless.

The Lego Sequencer will use the Lego Mindstorms EV3 Intelligent Brick as a processor to do 3 things: control the motor that spins the arm, receive the data from the EV3 Color Sensor, then play the sounds. Of course you can match your preferred sound notes to different colors so that you create your own beats.

  1. Build Your Lego Music Sequencer

The music sequencer is a really simple build. You’ll construct a base with a motor to rotate an arm, and mount at the arm the EV3 Color sensor where it can detect the color of Lego brick. Then you’ll hook up the EV3 brick for the brain. Just follow the following instructions of Damien Kee model.

https://www.damienkee.com/storage/EV3_Sequencer_build.pdf

  1. Program the EV3 brick - Drop Some Robotronic Beats

Program in Python the Lego Mindstorms EV3 Intelligent Brick as a processor to do 3 things: control the motor that spins the arm, receive the data from the EV3 Color Sensor, then play the sounds.

Options: With the up/down buttons you may control the speed of the spinning arms, and with the middle button you may stop the rotation to allow you to add and remove the colored bricks.

Each color of brick will be recognized by the software as the trigger for one of 5 specific notes of your choice. Just swap, delete, and remix the bricks to rearrange your beats!

Note: You may get lots of false positive triggers, especially at higher speeds, so you have to do a lot of attempts for an acceptable solution.

For short help on Python programming, look at appendix D.

  1. Going Further

Want to use your Sequencer as an input device for amplifiers or for recording? The EV3 brick has no audio out jack. But with its built-in Bluetooth, Wi-Fi and USB, there has got to be a way to get an output other than the speaker.

Grading Rubric

A

  • Lego guitar, piano and sequencer made correctly

  • 5 guitar challenges implemented without error

  • Table complete on Google Docs with correct calculations and no information missing

  • Group worked cooperatively and independently from other groups

B

  • Two of the either Lego guitar or piano or sequencer made correctly

  • 4 guitar challenges implemented without error

  • Table complete on Google Docs with correct calculations and 1-2 pieces of information missing

  • Group worked cooperatively and independently from other groups

C

  • One of the either Lego guitar or piano or sequencer made correctly

  • 3 guitar challenges implemented without error

  • Table complete on Google Docs with correct calculations and 1-2 pieces of information missing

  • Group worked fairly cooperatively and independently from other groups

D

  • Piano made correctly

  • 2 guitar challenges implemented without error

  • Table complete on Google Docs with correct calculations and 3-4 pieces of information missing

  • Group did not work cooperatively and independently from other groups

F

  • Piano made correctly

  • 1 guitar challenge implemented without error

  • Table incomplete on Google Docs

  • Group did not work cooperatively and independently from other groups

Theory

Note Schemes

There are a number of different scales in music! Here are some five note scales. Use these schemes for your guitar or piano, or create your own!

Blues Scale = A, C, D, D#, E (This is used in Jazz)

Pentatonic Scale = A, B, D, E, F# (used in a lot of traditional music all around the world)

Table of Notes and Frequencies

Note

Frequency

A

440

A#

466

B

494

C

523

C#

554

D

587

D#

622

E

659

F

698

F#

740

G

784

G#

830

A

880

If you would like to use other pitches, use the ratio to figure out their frequencies.

Appendix A

This is a guide on how to use Audacity to measure the period of a sound.

Step 1:

Open Audacity and make sure you have the measurement setting on the bottom set to ”Length.”image1

Step 2:

Set the measurement units to ”hh:mm:ss + milliseconds.”image2

Step 3:

Press the record button in the upper left side of the screen. Make a long, steady sound and press the stop button. With your mouse, select the middle of the recorded sound.

image3

Step 4:

Zoom in on the display. You can use the quick keys to do this, press ”Ctrl” and ”1.”

image4

Step 5:

Zoom in until you can see the specific sound waves.

image5

Step 6:

With your mouse, highlight 10 repetitions of the period. Read off the amount of time it took for the sound wave to complete the 10 periods. Then divide this number by 10 to get the actual period.

image6

Appendix B

Make Music - Happy Birthday Song

The two commands you must use when making music are: the Tone command and the Wait command. If you want to program a sophisticated song, you need to use subprograms (functions/procedures) to create the pieces and then link the pieces together. The syntax and semantics of sub-programs and Tone/Wait commands for EV3 Python can be found below in Appendix D.

The Main Program for Happy Birthday in pseudocode is as follows. We use a % to indicate a comment.

Repeat forever

Sub-program: hbd1 % Happy birthday

Wait 0.6

Sub-program: toyou1 % to you.

Wait 1.2

Sub-program: hbd1 % Happy birthday (same as first one)

Wait 0.6

Sub-program: toyou2 % to you.

Wait 1.2

Sub-program: hbd2 % Happy birthday,

Wait 0.6

Sub-program: hbd3 % happy birthday,

Wait 0.6

Sub-program: hbd4 % happy birthday,

Wait 0.6

Sub-program: toyou3 % to you.

Wait 1.2

Wait 0.6 % Quarter note rest before repeat.

End repeat

The sub-programs \ **hbd1, hbd2, hbd3, hbd4, toyou1,
toyou2,** and **toyou3** are:

Sub-program: hbd1

Tone 526 for 0.2 % Ha-

Wait 0.3

Tone 526 for 0.2 % ppy

Wait 0.3

Tone 587 for 0.5 % birth

Wait 0.6

Tone 526 for 0.5 % day

End of hdb1

Sub-program: toyou1

Tone 699 for 0.5 % to

Wait 0.6

Tone 660 for 1.1 % you.

End of toyou1

Sub-program: toyou2

Tone 785 for 0.5 % to

Wait 0.6

Tone 699 for 1.1 % you.

End of toyou2

Sub-program: hbd2

Tone 526 for 0.2 % Ha-

Wait 0.3

Tone 526 for 0.2 % ppy

Wait 0.3

Tone 1047 for 0.5 % birth

Wait 0.6

Tone 880 for 0.5 % day

End of hdb2

Sub-program: hbd3

Tone 880 for 0.2 % Ha-

Wait 0.3

Tone 880 for 0.2 % ppy

Wait 0.3

Tone 693 for 0.5 % birth

Wait 0.6

Tone 660 for 0.5 % day

End of hdb3

Sub-program: hbd4

Tone 466 for 0.2 % Ha-

Wait 0.3

Tone 466 for 0.2 % ppy

Wait 0.3

Tone 440 for 0.5 % birth

Wait 0.6

Tone 349 for 0.5 % day

End of hdb4

Sub-program: toyou3

Tone 392 for 0.5 % to

Wait 0.6

Tone 349 for 1.1 % you.

End of toyou2

If you want the music faster or slower, scale things appropriately.

Appendix C

Lego Guitar Python Code

    #!/usr/bin/env python3

    # so that script can be run from Brickman

    from ev3dev.ev3 import *

    import ev3dev.ev3 as ev3

    from time import sleep

    import random

    ev3.Sound.speak('Lets rock n roll!').wait()

    Leds.set_color(Leds.LEFT, Leds.GREEN)

    Leds.set_color(Leds.RIGHT, Leds.GREEN)

    m = MediumMotor('outD')

    ts = TouchSensor('in1')

    IR = InfraredSensor('in4')

    btn = Button()

    start = False

    print('Press Up or Down for long/sort duration of notes')

    while not start:

            if btn.up:

                    start = True

                    dur = 300

            if btn.down:

                    start = True

            dur = 150

    Nto = 262 #note D

    Re = 294 #note E

    Mi = 330 #note F

    Fa = 350 #note G

    Sol = 393 #note H

    La = 440 #note A [basic frequency 440Hz]

    Si = 495 #note B

    Nto2 = 524 #note c

    while start:

            d = IR.value()

            x = ts.value()

            if x == 0:

            if d<11:

                    Sound.tone(Nto, dur).wait()

            elif d<20:

                    Sound.tone(Re, dur).wait()

            elif d<29:

                    Sound.tone(Mi, dur).wait()

            elif d<38:

                    Sound.tone(Fa, dur).wait()

            elif d<47:

                    Sound.tone(Sol, dur).wait()

            elif d<56:

                    Sound.tone(La, dur).wait()

            elif d<65:

                    Sound.tone(Si, dur).wait()

else:

  Sound.tone(Nto2, dur).wait()

Appendix D

Short help on Python programming

Useful commands needed in this lesson

Connect medium motor to port D m = MediumMotor(‘outD’)

Plug a touch sensor into any sensor port ts = TouchSensor()

Here ts is the name we choose to use for the sensor. You can use any name you wish. This command will search the ports for a Touch Sensor and save its location if it finds one. You will get an error if it doesn’t find one. If you have more than one and want to specify:

ts = TouchSensor(‘in1’) Reading the Touch Sensor ts.value()

This function will return a numeric value. Since this is a touch sensor, there are only two possible values: 0 tells you the button is not currently pushed, 1 tells you the button is pushed.

You could have the Touch Sensor and Large Motor interact using the following simple program. Plug an infrared sensor into any sensor port IR = InfraredSensor()

Here IR is the name we choose to use for the sensor. Reading the distance of the IR sensor d = IR.value()

The distance is in cm. EV3 color sensor in COL-COLOR mode When the EV3 color sensor is in COL-COLOR mode it tries to recognize the color of standard Lego bricks placed about 5-6mm in front of the sensor (the distance is critical) and returns a corresponding integer value between 0 (unknown) and 7 (brown). The program below reads the integer once per second, converts it into the corresponding text string using a tuple and displays the string in the console. Press the touch sensor button for at least a second to stop the program.

To make the program speak the colors as well as displaying their text strings, uncomment the line highlighted in blue. This will slightly increase the time between measurements as it includes a wait() function to ensure that the speech was not interrupted.

#!/usr/bin/env python3

# so that script can be run from Brickman

from ev3dev.ev3 import \*

from time   import sleep

# Connect EV3 color and touch sensors to any sensor ports

cl = ColorSensor()

ts = TouchSensor()

# Put the color sensor into COL-COLOR mode.

cl.mode='COL-COLOR'

colors=('unknown','black','blue','green','yellow','red','white','brown')

while not ts.value():    # Stop program by pressing touch sensor button

    print(colors[cl.value()])

    #Sound.speak(colors[cl.value()]).wait()

    sleep(1)

Sound.beep()

Setting the two LED colors of the clever brick Leds.set_color(Leds.LEFT, Leds.GREEN)

There are two LEDs [Leds.LEFT or Leds.RIGHT] and five different colors [Leds.GREEN, Leds.RED, Leds.YELLOW, Leds.ORANGE, Leds.AMBER] If-statement

#!/usr/bin/env python3

if <condition1>:

  <commands1>

elif <condition2>:

  <commands2>
    …………………

else:

  <commandsN>

We can put as many elif as we want.

elif and else parts are optional

Loop (while-statement)

#!/usr/bin/env python3

while <condition1>:

  <commands>

The loop ends when the condition becomes false. If it is false from the beginning there is no execution of the <commands> Random number (random function)

#!/usr/bin/env python3

r = random.randint(0,4)

This command returns in variable r a random number between 0 and 4 Select an element of a list (Leds.GREEN, Leds.RED, Leds.YELLOW, Leds.ORANGE, Leds.AMBER)[2]

In the example the list contents the 5 LED colors and we selected the 3rd one Play a SINGLE tone To play a SINGLE tone of given frequency (Hz) and duration (milliseconds), use

Sound.tone(frequency, duration)

Example:

#!/usr/bin/env python3

from ev3dev.ev3 import \*

# Play a SINGLE 1500Hz tone for 2 seconds

# Wait for the tone to finish playing before continuing

**Sound.tone(1500, 2000).wait()**

Play a SEQUENCE of tones

To play a SEQUENCE of tones, use

Sound.tone(tone_sequence)

The tone_sequence parameter is a list of tuples, where each tuple contains up to three numbers. You should know by now that a tuple is like a list except that a list can be modified and a tuple cannot. The first number is frequency in Hz, the second is duration in milliseconds, and the third is delay in milliseconds between this and the next tone in the sequence.

#!/usr/bin/env python3

from ev3dev.ev3 import \*  # needed for Sound

# Play a 3000Hz tone for 2 seconds and then wait 0.4 seconds

# before playing the next tone, 800Hz for 1.8 seconds

# followed by a 2 second delay

# Wait for the sequence to finish playing before continuing

Sound.tone([(3000, 2000, 400),(800, 1800, 2000)]).wait()

User-Defined Procedures A Function is a series of Python statements begins by a def, followed by the function name and enclosed in parenthesis. A Function may or may not return a value. A Function procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). If a Function procedure has no arguments, its def statement should include an empty set of parentheses (). Parameters can also be defined within the parenthesis. The parentheses are followed by a colon (:) to end the first line.

he end of the function is marked by the loss of whitespace in the next line of the code (ending the code block). It is common practice to use a return statement followed by the argument to return a value. You may also finish a function with a return statement and a simple colon (;).

In the following example, the Celsius def calculates degrees Celsius from degrees Fahrenheit. When the def is called from the ConvertTemp def procedure, a variable containing the argument value is passed to the def. The result of the calculation is returned to the calling procedure and displayed in a message box.

def Celsius (fDegrees):

    _Celsius = (fDegrees - 32) * 5 / 9

    return _Celsius;

    # Use this code to call the Celsius function*

    temp = raw_input("Please enter the temperature in degrees F.", 1)

    MsgBox "The temperature is " & Celsius(temp) & " degrees C."

GLOSSARY (from Wikipedia)

Word Definition

Frequency Frequency is the number of occurrences of a repeating event per unit of time. Accidentals Letter names are modified by the accidentals. The sharp sign ♯ raises a note by a semitone or half-step, and a flat ♭ lowers it by the same amount. In modern tuning a half step has a frequency ratio of 12√2, approximately 1.059. The accidentals are written after the note name: so, for example, F♯ represents F-sharp, B♭ is B-flat, and C♮ is C natural (or C). Octave In music, an octave (Latin: octavus: eighth) or perfect octave (sometimes called the diapason) is the interval between one musical pitch and another with half or double its frequency. Pitch Pitch is a perceptual property of sounds that allows their ordering on a frequency-related scale, or more commonly, pitch is the quality that makes it possible to judge sounds as “higher” and “lower” in the sense associated with musical melodies. Robotics Robotics is an interdisciplinary branch of engineering and science that includes mechanical engineering, electronic engineering, information engineering, computer science, and others. Robotics deals with the design, construction, operation, and use of robots, as well as computer systems for their control, sensory feedback, and information processing. IR sensor A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors.

Next Section - Integrating liquid handling chemistry