Screen, buttons, LED, speaker

The LCD Screen (see also the manual)

The EV3 has a black and white screen with a resolution of 178 x 128 pixels. The top-left pixel has coordinates (0,0) and the bottom-right pixel has coordinates (177,127).

EV3 Basic has commands to do the following (see the manual for details). The color argument can be 0 (white) or 1 (black). Black would be the normal choice.
  • LCD.BmpFile(color, x, y, filename) draws a bitmap file to the display, see below.
  • LCD.Clear() clears the screen
  • LCD.Circle(color, x, y, radius) draws a circle in the given color.
  • LCD.FillCircle(color, x, y, radius) draws a filled circle with a given color.
  • LCD.Rect (color, x, y, width, height) draws an outline of a rectangle with a given color.
  • LCD.FillRect (color, x, y, width, height) draws a filled rectangle with a given color.
  • LCD.InverseRect (x, y, width, height) inverts the colors of all pixels inside a rectangle
  • LCD.Line (color, x1, y1, x2, y2) sets a straight line of pixels to a given color.
  • LCD.Pixel (color, x, y) sets a single pixel on the display to a given color.
  • LCD.Text (color, x, y, font size, text) writes a given text (or number) in a given color and a given font size to the display
  • LCD.Write (x, y, text) writes a given text (or number) in black and size 1 (medium) to the display (no control over the colour or the font size). 
  • LCD.StopUpdate () memorizes all subsequent changes to the display instead of directly drawing them.
  • LCD.Update () draws all changes to the display that have happened since the last call to StopUpdate(). 
Programming courses traditionally start with an example showing how the message "Hello World" can be displayed:

LCD.Clear()
LCD.write(45,60,"Hello World")  'First letter will be at (45,60) on LCD
Program.Delay(10000)    ' wait 10 seconds before closing

When the command LCD.write is used to display text, as above, the text size is always medium. When the command LCD.text is used to display text, you can choose between three text sizes: 0 (small), 1 (medium) and 2 (large). Here is an example using the large font (size 2):

LCD.Clear()
LCD.text(1,0,55,2,"Hello World")      '1=black,  display at (0,55),  2=size
Program.Delay(10000)       'Wait 10 seconds before closing

Sizes 0 and 1 are actually the same size but the size 0 text is skinnier and harder to read. Using small or medium sized text, each line can accommodate 22 characters and I recommend you allow at least 12 pixels per line vertically - you will then be able to use 10 lines. Large characters are twice as wide and twice as tall as small or medium characters so each line can accommodate only 11 large characters and you should allow at least 20 pixels vertically for each line which will allow you to display 6 lines of text. The choice of 'x' coordinate for the first character of the text string depends on the length of the string - I suggest you use trial and error. To display a single line of text half-way down the screen you should use a 'y' value of about 55. 

If you want to display several lines of text it is a good idea to pause the updating of the LCD screen until all the text has been written. This will prevent flickering. Use StopUpdate and Update for this, as demonstrated in many example programs on this site.

The 'Example Code' lower down of this page will introduce a couple of the most basic LCD instructions.

To learn how to display bitmap graphics on the LCD, see the section 'Custom sounds and Images' lower down this page.

Buttons (see also the manual)

The EV3 brick has 6 buttons, five of which can be referred to in EV3 Basic programs. EV3 Basic refers to these buttons as U(p), D(own), L(eft), R(ight), and E(nter). EV3 Basic does not use the remaining button, the Back button, except to terminate the program. There are only four EV3 Basic properties and functions for buttons:
  • Buttons.Current returns the buttons that are pressed at the exact moment that the property is called. For example, it might return the string "LU".
  • Buttons.Wait() waits until at least one button is clicked.
  • Buttons.GetClicks() checks which buttons were clicked since the last call to GetClicks and returns a text containing their letters. 
  • Buttons.Flush() removes any clicked-state of all buttons. Subsequent calls to GetClicks will only deliver the buttons that were clicked after the flush.
Here is a program that will wait for you to press a button then display the corresponding letter:

Buttons.Wait()
LCD.Clear()
LCD.Text(1,70,35,2,"You")
LCD.Text(1,35,55,2,"pressed")
LCD.Text(1,80,85,2,Buttons.GetClicks())
Program.Delay(6000)  'wait 6 seconds  (6000 milliseconds)

The LED

The EV3 brick has an LED light underneath the buttons. The LED can glow green, red or orange. EV3 Basic has only one command for working with the LED:
EV3.SetLEDColor (color, effect) which sets the color of the brick LED light and the effect to use for it.
  • Color can be "OFF", "GREEN", "RED" or "ORANGE".
  • Effect can be "NORMAL", "FLASH" or  "PULSE".

The Speaker (see also the manual)

Here are the most important commands that you can use with the EV3's speaker. By default, the program will NOT pause program execution until the sound has finished playing. Therefore if you want the program to pause you will need to use the Speaker.Wait() command.

Speaker.Tone(volume, frequency, duration) plays a simple tone of defined frequency. Volume can be 0 - 100. Frequency (in hertz) can be 250 - 10000. Duration is in milliseconds. 

Speaker.Note(volume, note, duration) is the same as Speaker.Tone except that the 'tone' is replaced by a 'note'. 'Note' can be "C4" to "B7" or half-tones like "C#5".

Speaker.Play(volume, filename) plays a sound from a sound file stored on the brick. Filename is the name of the sound file without the .rsf extension. The sound file must be in Lego's special RSF format - such files can only be created by the Lego software. For more details, see the section 'Custom Sounds and Images' lower down this page.

Speaker.Stop() stops any currently playing sound or tone.

Speaker.Wait() waits until the current sound has finished playing.

Example Code

Here is an example program which introduces some of the commands mentioned above. You can copy and paste this program into EV3 Basic (Small Basic with the EV3 extension). Tip: when pasting code into Small Basic, and from time to time, right-click the code window and choose 'Format Program' to adjust the indentation and make the code easier to read.

'*************************************************************************************
LCD.Clear()
LCD.Write(15,55, "Press buttons other") 
LCD.Write(40,70, "than 'Back'")

While "True"
 Program.Delay(200)
 'check five times every second whether a button has been pressed
  
  click = Buttons.GetClicks()   'the 'click' variable contains letter(s) 
'corresponding to which button(s) was/were pressed since the 
'last time it was checked
  If click <> "" Then  'if a button has been pressed then clear the screen
    LCD.Clear() 
  endif
  
  If Text.IsSubText(click, "U") then  'If the character string in 'click' contains a "U"
    LCD.Write(0,0, "This is written at") 
    LCD.Write(0,10, "(0,0) using LED.Write") 
    EV3.SetLEDColor ("OFF", "Normal")
    Speaker.Tone(100, 500, 400)  'Play a 500 Hz tone for 0.4s at full volume
    
  ElseIf Text.IsSubText(click,"D") then
    LCD.Text(1, 0,12, 0, "This is written at") 
    LCD.Text(1, 0,24, 0, "(0,12) using LED.Text") 
    LCD.Text(1, 0,36, 0, "in size 0 (small).") 
    EV3.SetLEDColor ("RED", "Flash")
    Speaker.Note(50, "D2", 400)  'Play the note "D2" for 0.4s at 50% volume
    
  ElseIf Text.IsSubText(click,"L") then
    LCD.Text(1, 0,12, 1, "This is written at") 
    LCD.Text(1, 0,24, 1, "(0,12) using LED.Text") 
    LCD.Text(1, 0,36, 1, "in size 1 (medium).") 
    EV3.SetLEDColor ("GREEN", "Pulse")
    
  ElseIf Text.IsSubText(click,"R") then
    LCD.Text(1, 0,20, 2, "This is") 
    LCD.Text(1, 0,40, 2, "made using") 
    LCD.Text(1, 0,60, 2, "LED.Text in") 
    LCD.Text(1, 0,80, 2, "size 2") 
    LCD.Text(1, 0,100, 2, "(large).") 
    EV3.SetLEDColor ("ORANGE", "Normal")
    
  ElseIf Text.IsSubText(click,"E") then
    LCD.Write(0,0,"Circle(1,50,70,30)")
    LCD.Circle(1, 50, 70, 30)  ' color,x,y,radius
    LCD.Write(0,12,"FillRect(1,90,80,60,40)")
    LCD.FillRect (1, 90, 80, 60, 40)   ' color,x,y,width,height
    LCD.Write(0,24,"Line(1,100,50,160,60)")
    LCD.Line(1,100,50,160,60)   ' color,x1,y1,x2,y2
    for i=1 to 3
      EV3.SetLEDColor ("RED", "Normal")
      Program.Delay(500)  'wait 0.5 seconds (500 milliseconds)
      EV3.SetLEDColor ("Green", "Normal")
      Program.Delay(500)
      EV3.SetLEDColor ("Orange", "Normal")
      Program.Delay(500)
    endfor
  endif  
EndWhile

'************************************************************

Custom Sounds and Images

The EV3 can only play sound files in the rsf format and can only display image files in the rgf format

The Lego EV3 software includes numerous sound and image files in these special formats. If you have the education edition then the sound files in RSF format can normally be found (in several folders) in this location (the 'Program Files (x86)' folder may also be called simply 'Program Files'):
C:\Program Files (x86)\LEGO Software\LEGO MINDSTORMS Edu EV3\Resources\BrickResources\Education\Sounds\files\

If you have the home edition, look here:
C:\Program Files (x86)\LEGO Software\LEGO MINDSTORMS EV3 Home Edition\Resources\BrickResources\Retail\Sounds\files\

The image files in RGF format can be found (in several folders) in this location if you have the education edition:
C:\Program Files (x86)\LEGO Software\LEGO MINDSTORMS Edu EV3\Resources\BrickResources\Education\Images\files\
or, if you have the home edition, here:
C:\Program Files (x86)\LEGO Software\LEGO MINDSTORMS EV3 Home Edition\Resources\BrickResources\Retail\Images\files\

The total size of the built-in sounds is 1.16MB and the total size of the RGF image files is about 0.45MB. The EV3 has a generous amount of storage (64MB RAM + 16MB Flash) so it is very possible and reasonable to copy all the built-in sounds and images to the EV3 brick using EV3 Explorer so that they are always available to your EV3 Basic programs.

We suggest that you first create two folders 'Sounds' and Images' on your PC and then copy the standard Lego RSF sound files and RGF image files into those folders. Copy only the RGF image files to the Images folder, not the BMP image files. A few of the RGF image files are tiny and should not be copied into the Images folder : Accept 1, Accept 2, Alert, Box, Busy 0, Busy 1, Decline 1, Decline 2, Dot empty, Dot full, EV3 small, Play, Slider 1-8. Once you have removed those small images you will be left with this selection.

To keep the folder hierarchy as simple as possible, we recommend that you do NOT keep the original subfolder structure (Animals, Mechanical, etc...), just put the image files directly in the Images folder and the sound files directly in the Sounds folder. 

Then you should create (using EV3 Explorer) a folder called 'Sounds' and a folder called 'Images' inside the 'prjs' folder on the brick, which is the 'home' directory as far as EV3 Basic is concerned. Don't forget that the brick's operating system is case-sensitive since it is a version of Linux. Then copy (download) all the sound files and image files from the two folders on the PC into the corresponding two folders that you just created on the brick. 

Even after you copy the sound and image files to the brick it is a good idea to keep the two folders on your PC because if you update the brick's firmware one day all your files on the brick will be lost but it will be easy to restore the sound and image files from the PC.

Once you have downloaded all the sound and image files as instructed above you can play/display them in EV3 Basic with commands such as:

LCD.Clear()
LCD.BmpFile(1, 0, 0, "Images/Angry")  'color 1 for black, x=0,y=0
Speaker.Play(100, "Sounds/T-rex roar") 'do NOT include the file name extension
Program.Delay(10000)
'wait 10 seconds so the image can be seen and the sound heard

If you want to use other sound or image files then the best way to convert them into those EV3-compatible formats is to use the Lego EV3 software (for sound files, it may also be possible to use a program called WAV2RSO and then change the extension from rso to rsf - these formats are in fact identical, I believe.) Once you've done that you can copy the sound and image files into the EV3 using EV3 Explorer (when you install the EV3 Basic extension, the EV3 Explorer program is installed in a folder called 'EV3Explorer' which is located in the same folder that the EV3 extension files are copied to, namely 
Local Disk (C:) \ Program Files (x86) \ Microsoft \ Small Basic \ lib.)

When you install the EV3 extension (version 1.0.4 or later) some example programs are included in the download and one of the example programs, called GraphicsAndSounds.sb, demonstrates the use of a custom sound and image in EV3 Basic. Before you can use the program, you will need to copy (using EV3 Explorer), the corresponding sound and image files into a folder called GraphicsAndSounds (case sensitive) which must be located inside the prjs folder (EV3 Explorer opens this folder on the EV3 by default). Here is the program:

' This example uses graphics and sound resources that must be already
' present on the EV3 in order to access them from the program. 
' In this chase, a bitmap file (yamyam.rgf) and a sound file (emerald.rsf)
' are expected to exist in a sub-folder "GraphicsAndSounds'  inside the 
'  "prjs" folder (you can create such a folder and transfer the files from the examples 
' to the  EV3 with the EV3 Explorer. Contrary to the Small Basic convention,
' filenames for the EV3 are case-sensitive (because it is a Linux file system).
'
' As long as the EV3 Explorer does not support file format conversion, you
' will probably still need the standard lego software to create your own sounds
' and bitmaps. 

LCD.Clear()
LCD.BmpFile(1, 40,10, "GraphicsAndSounds/yamyam")
' The first three arguments above are the colour to be used 
'(0=white, 1=black) and the coordinates of the top-left corner
'of the image

Speaker.Play(100, "GraphicsAndSounds/emerald")  ' volume=100

Program.Delay(100000)   ' 100 seconds

Comments