Hardware
Learn how to use EV3 Basic with the various sensors and motors. You have probably already studied this page about sensors and this page about motors. The following exercises are the EV3 Basic versions of the 'Hardware' exercises in the 'Robot Educator' section of the standard Lego software (education version).
Many of the exercises on this page assume that you have copied the standard Lego sound and images files to the brick in accordance with the instructions on this page. When you try to play or display the sounds and images stored in the brick, be aware that the brick has a Linux operating system and therefore folder and file names on the brick are case-sensitive (this is not the case for Small Basic itself).
Brick Sound, LED, Display, Buttons:
Brick Sound
Objective:
Play all these sounds to completion at 100% volume:
'Fanfare' sound
A 440 Hz tone for 1 second
'Boing' sound (play repeatedly)
After three seconds repeating the 'Boing' sound, stop all sound.
Solution:
Speaker.Play(100, "Sounds/Fanfare") 'volume = 100
Speaker.Wait() 'Wait for the sound to finish playing
Speaker.Tone(100, 440, 1000) '440 Hz, 1000 milliseconds
Speaker.Wait()
StartTime=EV3.Time 'time in milliseconds since program started
While EV3.Time<(StartTime+3000)
Speaker.Play(100, "Sounds/Boing")
Speaker.Wait()
EndWhile
Notes:
See the yellow-highlighted note at the top of this page.
Speaker.Play and Speaker.Tone do not cause program execution to pause until the sound has finished, hence the inclusion of the Wait commands above.
The above solution is neat and simple but it won't interrupt the playing of the Boing sound after three seconds of playing that sound - it just won't play it again. To actually interrupt the sound, more complex code might be needed - I may include that later.
Brick Status Light
Objective:
Wait 2 seconds.
LED glows steady red for two seconds.
LED pulses orange for two seconds.
Solution:
Program.Delay(2000) '2000 milliseconds
EV3.SetLEDColor("Red", "Normal")
Program.Delay(2000)
EV3.SetLEDColor("Orange", "Pulse")
Program.Delay(2000)
Notes:
During the first two seconds I would expect the LED to pulse green since this is the default but I actually get steady green. A minor bug that manifests itself when the first line in a program is Program.Delay()?
Brick Display
Objective:
Display the standard Lego 'Thumbs up' image.
Wait 2 seconds.
Display the text string 'EV3' in the centre of the LCD display in the large font.
Wait 2 seconds.
Solution:
LCD.Clear()
LCD.BmpFile(1,0,0,"Images/Thumbs up") '1=black, x=0, y=0
Program.Delay(2000) '2000 milliseconds
LCD.Clear()
LCD.Text(1,65,55,2,"EV3")'1=black, x=65, y=55, size=2 (large)
Program.Delay(2000) '2000 milliseconds
Notes:
See the yellow-highlighted note at the top of this page.
The x coordinate for the first text character of the text string depends on the length of the string - I suggest you use trial and error.
For the text display, y=55 is a good value to place a single line of text at approximately the vertical center of the LCD screen. Remember that each line of text occupies 10 pixels vertically in the small and medium fonts (sizes 0 and 1) and 20 pixels vertically in the large font (size 2).
Brick Buttons
Objective:
Wait for the bottom button to be pressed.
Display the standard Lego 'Sad' image.
Wait for the center button to be pressed.
Display the standard Lego 'Sick' image.
Wait for the top button to be pressed.
Display the standard Lego 'Smile' image.
Wait for 2 seconds.
Solution:
While Buttons.GetClicks()<>"D" 'Down button
EndWhile
LCD.BmpFile(1,0,0,"Images/Sad")
While Buttons.GetClicks()<>"E" 'Enter button
EndWhile
LCD.BmpFile(1,0,0,"Images/Sick")
While Buttons.GetClicks()<>"U" 'Up button
EndWhile
LCD.BmpFile(1,0,0,"Images/Smile")
Program.Delay(2000)
Notes:
This program uses image files so see the yellow-highlighted note at the top of this page.
Buttons.GetClicks() checks which buttons were clicked since the last call to GetClicks and returns a text containing their letters.
For other button functions see this page.
Motors:
Large Motor
Objective:
Make the motor on port D turn 1 rotation at 50% speed, then apply the brake.
Wait 1 second.
Make the same motor turn 360° at -50% speed (backwards), then apply the brake.
Wait 1 second.
Make the same motor turn at 10% speed for 1 second, then apply the brake.
Solution:
'Attach a motor to port D
Motor.Move("D",50,360,"True") ' 'True' refers to the brake
Program.Delay(1000)
Motor.Move("D",-50,360,"True") 'Make speed negative, not angle
Program.Delay(1000)
Motor.Start("D",10)
Program.Delay(1000)
Motor.Stop("D","True")
Notes:
EV3 Basic makes no distinction between the large motor and the medium motor.
Important: To make a motor rotate backwards always make the speed negative rather than the angle.
'Power' in the standard Lego software is called 'speed' in EV3 Basic. EV3 Basic also has a 'power' setting, but this is for experts only.
EV3 Basic uses degrees for turn angles - you cannot specify the number of rotations. Therefore you have to multiply the number of rotations by 360 to convert to degrees - you can do this in the code, of course.
Medium Motor
Objective:
Turn on the motor on port A at 12% speed.
Wait 1 second.
Stop the motor and apply the brake.
Wait 1 second.
Make the same motor turn 360° at 50% speed, then apply the brake.
Solution:
'Attach a motor to port A
Motor.Start("A",12)
Program.Delay(1000)
Motor.Stop("A","True") '"True" refers to the brake being applied
Program.Delay(1000)
Motor.Move("A",50,360,"True")
Sensors:
This page adheres to the convention that sensors are connected to the numbered ports as follows (this is a convention, not an obligation):
1= touch 2 = gyro 3 = color 4 = ultrasound or infrared
To learn more about sensor modes and about whether to read the sensor value with Sensor.ReadPercent, Sensor.ReadRawValue or Sensor.ReadRaw see this page about sensors and the sensor appendix.
Touch Sensor
Objective:
Wait for the touch sensor button to be pressed.
Play the 'Ouch' sound to completion at 100% volume.
Solution:
Sensor.SetMode(1,0) 'Make sure sensor is in mode 0
While Sensor.ReadPercent(1)=0 'repeat while button not pressed
EndWhile
Speaker.Play(100,"Sounds/Ouch")'brick file names case-sensitive
Speaker.Wait() 'needed?
Notes:
See the yellow-highlighted note at the top of this page.
Gyro Sensor
The gyro sensor is not included in the 'home' version of the EV3 kit.
Important: Keep the gyro sensor and EV3 very still when connecting the cable and during start-up of the EV3, otherwise the gyro sensor reading will wander away from the correct reading.
Objective:
Wait for the angle measured by the gyro sensor to increase by 90°.
Play the 'Laser' sound to completion at 100% volume.
Solution:
'Attach gyro sensor to port 2
Sensor.SetMode(2,0) 'Make sure sensor is in mode 0: angle in degrees
While Sensor.ReadRawValue(2,0) < 90
EndWhile
Speaker.Play(100,"Sounds/Laser") 'brick file names case-sensitive
Speaker.Wait() 'needed?
Notes:
If you have problems with the gyro sensor it's helpful to continuously display the angle on the LCD. You can do that with the extended version of the program, below. If you notice that the reading is wandering even when the robot is still, then unplug and reconnect the sensor while keeping the sensor absolutely still.
Sensor.SetMode(2,0)
While Sensor.ReadRawValue(2,0) < 90
LCD.StopUpdate() 'Avoid flicker: don't update LCD until text is ready
LCD.Clear()
LCD.Text(1,70,55,2,Sensor.ReadRawValue(2,0))
LCD.Update()
EndWhile
Speaker.Play(100,"Sounds/Laser")
Speaker.Wait()
Color Sensor: Color
Objective:
Wait for the color sensor to detect blue.
Play the 'Blue' sound to completion at 100% volume.
Wait for the color sensor to detect yellow or red.
Play the 'Yes' sound to completion at 100% volume.
Wait for the color sensor to detect no colour.
Play the 'Thank you' sound to completion at 100% volume.
Solution:
In mode 2 (detect standard colors) the color sensor should be read with ReadRawValue. This will return a single value which is a color code for a standard Lego color. The codes are 0=unknown, 1=black, 2=blue, 3=green, 4=yellow, 5=red, 6=white, 7=brown.
'Connect color sensor to port 3
Sensor.SetMode(3,2) 'Set mode to 2: detect standard colors
While Sensor.ReadRawValue(3,0) <> 2 'blue
EndWhile
Speaker.Play(100,"Sounds/Blue") 'brick file names are case-sensitive
Speaker.Wait()
'4=yellow, 5=red
While Sensor.ReadRawValue(3,0) <> 4 and Sensor.ReadRawValue(2,0) <> 5
EndWhile
Speaker.Play(100,"Sounds/Yes")
Speaker.Wait()
While Sensor.ReadRawValue(3,0) <> 0 'no color
EndWhile
Speaker.Play(100,"Sounds/Thank you")
Speaker.Wait()
Notes:
See the yellow-highlighted note at the top of this page.
Mode 2 (detect standard colors) is very fussy about colors - 'official Lego colors' (i.e. Lego bricks) work best.
Color Sensor: Light
Objective:
Wait until the ambient light level exceeds 20%.
Make the LED start pulsing orange.
Wait 1 second.
Reset the LED (turn it off).
Solution:
'Connect color sensor to port 3
Sensor.SetMode(3,1) 'Set mode to 1: ambient light level
While Sensor.ReadPercent(3) < 20
EndWhile
EV3.SetLEDColor("Orange","Pulse")
Program.Delay(1000)
EV3.SetLEDColor("Off","Normal")
Ultrasonic Sensor
The ultrasonic sensor is included in the 'education' version of the EV3 kit but not in the 'home' version. The home version contains instead an infrared sensor which can (in mode 0) also be used to measure distance and it can therefore be used in place of the ultrasound sensor - see the next exercise.
To obtain the distance reading from the ultrasound sensor, Sensor.ReadRawValue(port number, 0) should be used. If the sensor is in mode 0 then the distance will be measured in mm (not cm). If the sensor is in mode 1 then the distance will be measured in tenths of an inch.
Objective:
This exercise assumes that an object, such as your hand, is being brought towards a stationary robot.
Wait until the object is less than 8 cm from the sensor.
Display the standard Lego 'Stop 1' image.
Wait 2 seconds.
Solution:
'Connect ultrasound sensor to port 4
Sensor.SetMode(4,0) 'set port 4 to mode 0: distance in mm (not cm)
While Sensor.ReadRawValue(4,0) >= 80 '8cm = 80mm
EndWhile
LCD.BmpFile(1,0,0,"Images/Stop 1") 'Files in brick are case-sensitive
Program.Delay(2000)
Notes:
Since this program uses a standard Lego image file, see the yellow-highlighted note at the top of this page.
Infrared Sensor
The education version of the EV3 kit does not contain an IR sensor and theretofore there are no exercises in the education version that use the IR sensor. This exercise, then, is a supplement for those of you that DO own the IR sensor (because you bought the home edition). This exercise is identical to the previous exercise except for the different sensor and the corresponding changes to the code.
Objective:
This exercise assumes that an object such as your hand is being brought towards a stationary robot.
Wait until the object is less than 8 cm from the IR sensor.
Display the standard Lego 'Stop 1' image.
Wait 2 seconds.
Solution:
'Connect IR sensor to port 4
Sensor.SetMode(4,0) 'set port 4 to mode 0: distance in cm (approx)
While Sensor.ReadPercent(4) >= 8
EndWhile
LCD.BmpFile(1,0,0,"Images/Stop 1")
Program.Delay(2000)
Notes:
Since this program uses a standard Lego image file, see the yellow-highlighted note at the top of this page.
Distance values will be approximate, especially if your reflecting object is dark-colored (distance estimate is based on reflected light intensity).