Cloud Driven DC Motor Control Using the S5D9 - Part 2

Originally posted on the Learn IoT Community site, Nov 2017, by community member Dan Kane 


In this tutorial, you will add direction control to the DC motor circuit you created in Part 1 of this tutorial. To do that, you’ll first customize widgets on the Renesas/Medium One dashboard, and modify one workflow. Next, you’ll wire the two direction control pins on the L293DNE to two GPIO pins on the S5D9 via the PMOD connector, and modify the binary in e2studio to add the functionality.


Part 1 of this tutorial must be completed first. Part 2 builds on what was done in Part 1.


In Part 1, none of the dashboard widgets or workflows were changed. We simply re-purposed the ‘blink’ widget to transmit PWM duty cycle % as a value from 0 – 100. For this tutorial we’ll clean up the widgets by eliminating those that we don’t need, and customizing the ‘blink’ widget. We’ll also add a boolean user input widget to control the forward/reverse functionality of the motor. This will require a small modification to the Python workflow associated with these widgets. In Part 1, we hard wired the L293DNE to run in one direction only. In Part 2, we’ll use GPIO pins to control the direction, so there will be a small modification to the circuit.

Step 1: Modify the Renesas/Medium One Dashboard Widgets

Navigate to the IoT Sandbox Dashboard and scroll down to the Dashboard User Input widgets. You’ll first delete the ‘Toggle’ and ‘Send Sample Report’ widgets since they are not needed. You’ll still use the ‘Blink’ widget to transmit DC motor speed, however, you’ll change the name so its function is more clear. Click on the configuration button and then un-check both the raw:toggle and raw:sample_report boxes. That will remove the widgets.



Navigate to ‘Config’ → ‘Data Streams’ and edit ‘raw Events.’

Replace the Label text ‘Blink’ with ‘Motor Speed’ next to the raw:blink tag.


Scroll down to the raw:toggle tag and change the Label text to ‘Motor Forward.’ Also, change the Use as: setting from Number to Boolean – Last Value. Finally, scroll all of the way down and click ‘Save Data Stream.’


Now, add a single user boolean input widget. This will control the motor direction.


Be sure to select the same user that you have selected for all of your other dashboard widgets and then click on the Tag Config button.

Check the box next to the Motor Forward label and then save.


Your widgets on the dashboard will now look like this:

Be sure to scroll all the way to the top and click on the disk icon to save the dashboard configuration.

Step 2: Modify the ‘Blink’ Worflow

Go to the Workflow Studio and de-activate (optional – the tutorial will still work) all of the workflows except “LED Toggle and Blink.”


Double click on the “LED Toggle and Blink” workflow to open it and then delete the raw:toggle trigger by clicking on the x. We need to delete it and then add it back again since when the original was created, toggle was a number and now it is boolean.

Now access the ‘Tags and Triggers’ menu and drag a copy of raw:toggle back to the same location and connect it to ‘in1.’


Finally, double click on the ‘Base Python’ module and replace the existing code with the following code. When done, click ‘Save and Activate.’

LED Toggle and Blink Workflow
This workflow allows you to control the LED on the board from the cloud. 

- Triggered by sending an event with format {"blink": n}, where 'n' is a number
- The red LED on the board will blink 'n' times

- Triggered by sending an event with format {"toggle": n}, where 'n' is a number 
- If 'n' is 1, the yellow LED on the board will turn on
- If 'n' is 0, the yellow LED on the board will turn off
- If 'n' is neither, the yellow LED on the board will switch states from on to off or vice versa

By default, we have only the red LED blink and only the yellow LED toggle. If desired, this
is customizeable. In this workflow, an event is sent to the board in the format of 'L2:' for
toggle or 'B4:' for blink. The '2' and '4' specify which LED to toggle/blink. Change this number to
the following values to change the LEDs controlled:
- Green: 1
- Yellow: 2
- Green + Yellow: 3
- Red: 4
- Green + Red: 5
- Yellow + Red: 6
- Green + Yellow + Red: 7

Last Updated: May 1, 2017

Author: Medium One

# November 3, 2017 Modification by DK
# Re-purpose toggle tag to control DC motor direction
# raw:toggle trigger is now boolean.  'true' = 1 = forward
# false = 0 = reverse
# Blink is a number between 0 and 100 which still blinks the red LED
# but now also sets the motor speed PWM duty cycle in %

import MQTT

if IONode.is_trigger('in1'):
    motor_forward = IONode.get_input('in1')['event_data']['value']
    if motor_forward == True:
        MQTT.publish_event_to_client('s5d9', 'L2:1')
        log("LED turned on, DC motor set to forward")
    else: # motor_forward == False
        MQTT.publish_event_to_client('s5d9', 'L2:0')
        log("LED turned off, DC motor set to reverse")
#    else:
#        MQTT.publish_event_to_client('s5d9', 'L2')
#        log("LED toggled")

elif IONode.is_trigger('in2'):
    blink = IONode.get_input('in2')['event_data']['value']
    MQTT.publish_event_to_client('s5d9', 'B4:' + str(blink))
    log("LED blinked " + str(blink) + " times")

Step 3: Configure Port 2, Pins 6 and 7 as GPIO Outputs

As a review, here is the PMOD schematic.

In this tutorial, you’ll use PMOD pins 8 and 9 to control the direction of the DC motor using the two control inputs on the LM293D. These two pins are connected to S5D9 Port 2, Pins 6 and 7.

In e2studio, make sure P2_6 is configured as a CMOS output.


Now repeat the process for P2_7. P2_6 was configured as “initial low”. This pin is configured as “initial high” so that the DC motor’s default state is forward (or reverse) depending upon how you connect the motor.


Step 4: Build an Updated Binary and Load It

In e2studio, navigate to net_thread_entry.c. This is the same file we edited in Part 1 of this tutorial. We are re-purposing the ‘toggle’ instruction to control the motor direction as well as the state of the yellow LED. The workflow code in step 2 above shows that ‘toggle’ messages are prefixed with an ‘L.’ Therefore, we’ll modify the S5D9 response to toggle messages in the case ‘L’ statement. Simply replace the entire ‘case L:’ statement in net_thread_entry.c with the code below:

        case 'L': {
            int val;
            sscanf(&p[1], "%d", &temp);
            temp_str = strchr(&p[1], ':');
            // no strnchr...

            if ((temp_str != NULL) && ((temp_str - p) < msglen)){
                if (sscanf(temp_str + 1, "%d", &val) == 1)
                    set_led((uint8_t)temp, (uint8_t)val);
                    //Begin DK DC Motor Drive Tutorial Modification #4
                    //The changes below set the motor drive direction according to whether the incoming instruction was
                    //L2:0 - turn the yellow LED off and set motor direction to reverse or
                    //L2:1 - turn the yellow LED on and set motor direction to forward
                    if (val == 1)
                        g_ioport.p_api->pinWrite(IOPORT_PORT_02_PIN_07, IOPORT_LEVEL_HIGH);
                        g_ioport.p_api->pinWrite(IOPORT_PORT_02_PIN_06, IOPORT_LEVEL_LOW);
                        g_ioport.p_api->pinWrite(IOPORT_PORT_02_PIN_07, IOPORT_LEVEL_LOW);
                        g_ioport.p_api->pinWrite(IOPORT_PORT_02_PIN_06, IOPORT_LEVEL_HIGH);
                    //End DK DC Motor Drive Tutorial Modification #4
            } else
                toggle_leds((temp >> 2) & 0x01, (temp >> 1) & 0x01, temp & 0x01);

Now, build the project and upload the new .srec file to the board using the bootloader as before.

Step 5: Build the Circuit and Test It

Starting with the circuit used in Part 1 of this tutorial, you’ll disconnect pin 2 of the L293D from +5V where it was hard wired and connect it to PMOD pin 9 which is connected to P2_7 on the S5D9 which is configured as a GPIO output initial high. Disconnect pin 7 of the L293D from GND and connect it to PMOD pin 8 which is connected to P2_6 which is configured as a GPIO output initial low. The final circuit is shown below.


Once everything is connected and running, navigate to the control widgets on the dashboard. Set the speed to maybe 20% and then experiment with toggling the ‘motor forward’ boolean widget. Clicking on the word ‘true’ or ‘false’ toggles it, and then press ‘Send Event.’ If the state has been changed, the motor will change direction.


Next Steps:

In Part 3 of this tutorial, we’ll configure the mobile app to control the motor speed and direction.