So far we have looked at control structures, variables and functions, etc. In the second entry of this course, we looked at how to communicate via serial port. Today we´re going to look at it in more detail. As well as using the serial port to debug our program and find out information, we will also give commands to our ZUM BT 328, or any Arduino board, using the serial monitor. For example, this allows us to send a vector to our robot indicating the direction and speed it should move at. We will also learn how to do this with an analogue signal using the Arduino PWN pins, which we will use to change the colour of the RBG LED. The PWM function of Arduino boards enables us to generate an analogue signal using digital signals. To find out more about it, you can visit the PWM reference page.

List of materials

  • ZUM BT-328 controller board or one compatible with Arduino.
  • Breadboard (or test board or protoboard).
  • RBG LED, common cathode in this case.
  • Cable jumper.

Electrical connections

On using a breadboard, if we are starting a project or the project is small, it might not matter, but if it keeps increasing in size, it´s important to use coloured cables to make it easier to understand the circuit visually. By using the RGB LED we can make the connections easier, for example, by using a red cable for the pin that controls the red colour of the RGB LED. We usually use a red cable for the live wires of the circuit and a black or brown cable for earth or GND (ground).  The LED module we are using in this example includes resistors to limit the current in the module itself, but if the one you are using doesn´t have any, make sure that you connect a resistor for each colour to avoid burning the LED. This diagram provides an example of how you can connect an LED without integrated resistors:

108With this project, we will connect the RGB LED and the ZUM BT-328 board as follows:


6 R
5 G
3 B

The code

As usual, we will start by declaring the variables of the pins:

We will also declare some variables that we will be using throughout the sketch:

In the delayLed variable, we store the time that passes in milliseconds during the transition between one LED value and the next.  We recommend using a very low value, as a delay  is used to make the transition smoother. As a result, we need to bear in mind that the greater the value, the longer it will take to make the transition, for example, for the value of 10 ms, the transition from black to white, or from (0,0,0)  to (255,255,255), takes 7.6 seconds to complete. The same transition with 5 ms takes 3.8 seconds to complete. Now we will use the setup function to start our program:

We start the serial port at setup and we will use it for communicating between Arduino and the computer. We also set the different pins used for each colour as output. Within the loop, we can find different methods that we haven´t seen until now; parseInt and constrain. The parseInt method is used together with the Serial with the Serial.parseInt(); command. It searches for data received via the serial port on the next chain of whole numbers until it finds a comma. You can view the reference page here. The constrain function is used to “restrict” a value to a range between two other values. This means that if the value is between the range of A-B, it will take its value. If lower, it will take the value of A or if it is higher, it will take the value of B. You can view the reference page here. The code for the loop is as follows:

Within the loop, we can see that fade is called, a function that we have created which allows us to smoothly change the LED from the present colour to the new colour. The fade function is defined after the loop, as follows:

    In this function we can distinguish between two cases:

    1. The next value is higher than the current value, so we need to increase the value of the LED.
    2. The next value is lower than the current value, so we need to reduce the value of the LED.

A command that we haven´t seen until now also appears in this function, analogWrite, which uses the PWM (pulse width modulation) capacities of our board. The PWM simulates an analogue signal using discrete pulsations from a digital signal. You can find more information on the PWM reference page.