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:

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:

Brick Status Light

Objective:

Solution:

Program.Delay(2000)   '2000 milliseconds

EV3.SetLEDColor("Red", "Normal")

Program.Delay(2000)

EV3.SetLEDColor("Orange", "Pulse")

Program.Delay(2000)

Notes:

Brick Display

Objective:

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:

Brick Buttons

Objective:

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:

Motors:

Large Motor

Objective:

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:

Medium Motor

Objective:

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:

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:

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:

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:

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:

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:

Color Sensor: Light

Objective:

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.

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:

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.

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: