C-3PO the Master Teacher

Hello, in this guide I will show you how to build a robot that can be used for teaching. The name of the robot is "C-3PO the Master Teacher". The robot is built from a PEZ Candy Dispenser. It can talk and understand speech. It is a great tool for children, who like the PEZ candy. The robot asks questions, and if the answer is correct it gives a candy. If the answer was wrong, it tells you the correct answer, but keeps the PEZ dispenser closed. Check out the following video to see it in action.

start the video

How does it work?

The robot is attached to a laptop via a USB cable. The laptop has a built in microphone and speaker. The speaker on the laptop is used to ask the questions. The microphone is used to record the results. If a result is correct, the robot is instructed through the USB cable to dispense a candy.

dialog example

What do I need to build it?

In order to build this robot you only need a few parts. You need a PEZ candy dispenser, a small servo motor, a small wire, a paper clip, an arduino nano and an USB cable to connect the robot to your laptop.

parts you need to build the robot
Figure 1 - Parts you need to build the robot

All of these parts can be purchased from e-bay.co.uk. If you are lucky, you might find some of the parts in your own house. The budget for the robot is 7.67 pounds. You can find a breakdown of the costs in the following table.

How much does it cost?

PartCost
PEZ candy dispenserGBP 2.29,-
SG90 servo motorGBP 1.39,-
6cm brown wireGBP 0.05,-
Paper clipGBP 0.05,-
Arduino nanoGBP 2.50,-
USB cableGBP 1.39
Total7.67

How much time do I need to build it?

For me it took about 4 hours to build it, but if you are not that expreienced I would say it will not take more then a day to make it work. For me it took about 1 hour to put the parts together, about 1 hour to setup the Arduino Nano, and about 2 hours to write the robot control program. If you follow the steps in this tutorial and you are expreienced, maybe you can do it in a very short time.

How can I build it?

To build the robot, follow these instructions. First you have to make the hardware, than you need to setup the software. Finally you will have to test it. Testing will be a lot of fun. Get started now!

Step 1 - Create the pull wire

To create the pull wire, you must connect the servo horn to the paper clip, and you must attach the wire with a knot. Check out the following picture. make sure the wire has sufficient length. A length of 6-8cm is a good choice.

the finished wire
Figure 2 - The finished wire

This pull wire is a key part. It will be connected to the head of the PEZ candy dispenser, and the servo horn end will be attached to the servo motor. When you will load candy into the dispenser the servo horn can be detached from the servo motor. The servo horn comes with the SG90 servo, so you don't have to worry if you don't have one at the moment.

Step 2 - Attach the pull wire to the PEZ dispenser

Once the pull wire is ready, you need to attach it to the PEZ candy dispenser. I have attached it using a hole. I have used a Dremel drill to drill the hole, and I have pushed the wire through, than I have tied a knot to the end, to make sure it stays where it is intended to stay.

connect the wire to the pez candy dispenser
Figure 3 - Connect the wire to the PEZ candy dispenser

If you want to build this robot from a different PEZ candy dispenser, make sure you choose one, where the head can be drilled, so you can attach the pull wire.

Step 3 - Prepare the servo motor

When you purchase an SG90 servo motor, the cable order is: black, red and orange. The black and red wires are used for supplying power to the robot, the orange wire is used for control. This cable order is not fit for our project, because the pinout of the Ardunio Nano has a different order. If we want to use the SG90 servo motor in our project we should change the cable order at the connector to the following: black, orange, red. In this order the orange control wire is in the middle.

modify the sg ninety servo motor
Figure 4 - Modify the SG90 servo motor

In the following picture you can see the wiring diagram of the robot. You can see where the black (ground), the red (5V) and the orange control wire is attached.

wiring diagram
Figure 5 - Wiring diagram

The most important part in this wiring diagram is the orange wire. The orange wire in this diagram is attached to PIN 11 of the Arduino nano. You will have to use this pin number when you upload the Arduino control software.

Step 4 - Glue it all together

The thing I enjoy the most, when I build a robot is the final assembly. When all the parts are ready, and I put everything together it is a great feeling. To assemble your robot, you have to glue the Arduino nano and the servo motor to your PEZ candy dispenser. Put the Arduino Nano at the bottom, and above it put the servo motor. Make sure the USB port of the Arduino Nano, and the motor head of the serve are facing the back side of the candy dispenser.

compilation of the robot
Figure 6 - Compilation of the robot

You can take a look at the following video to see how I glued my robot together. You might notice, that I have also attached stand to the bottom to make it more stable.

start the video

Step 5 - Setting up the software

You can install the software on any Windows 10 PC. I control my robot from my laptop. My laptop is an IBM Thinkpad t420. It has an English Windows 10 installed as the operating system. Note, that if you use another language, you might have to add the English language pack to windows, to have speech recognition available. English speech recognition is not included in non-English versions of Windows.

To control your robot, you will need to setup two software: the Arduino software package, which can be downloaded from Arduino.cc. The exact link is

https://www.arduino.cc/download_handler.php?f=/arduino-1.8.5-windows.exe

and you will need to install Ozeki 10, which can do the text to speech and speech to text conversion. Ozeki 10 will allow you to write your robot control software in C#.Net, which is a great programmin language. Ozeki 10 can be downloaded from the following URL:

https://ozeki.hu/p_231-download-ozeki-software-products.html

Once the Arduino and the Ozeki software is installed, you can plug your Robot into the USB port of your PC. Your PC will recognize the robot as a serial port e.g.: COM6.

requirements to control the robot
Figure 7 - Requirements to control the robot

Step 6 - Formatting the Arduino

Once the Arduino IDE is installed, you need to do two things: first you must format the Arduino, then you must upload the servo control software. To format the Arduino you can use the code from the Ozeki website (https://ozeki.hu/p_3110-eeprom-manager-format-for-arduino.html) or from the Arduino website (https://www.arduino.cc/en/Tutorial/EEPROMClear). I prefer to use the Ozeki code, because it will blink the led on the Arduino when it is finished, so I know, that I can proceed with the next step.

To format the arduino you have to select the "Arduino Nano" board model, and the correct serial port from the tools menu.

select the arduino nano board model
Figure 8 - Select the "Arduino Nano" board model

Once the board model and Port is selected, upload the following code, and wait for the LED on the Arduino to blink:

#include 
 
int a;
 
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
   
  for (uint16_t i = 0; i < EEPROM.length(); ++i) {
    EEPROM.update(i, 0);
  }
   
  for (uint16_t i = 0; i < EEPROM.length(); ++i) {
    a+=EEPROM.read(i);
  }
 
  if (a == 0){
    Serial.println("EEPROM is null! The process was successfull!");
  } else if (a > 0) {
    Serial.println("EEPROM is not null, please upload the code again!");
  }
}
 
void loop() {
  if (a == 0){
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
  }
}

Step 7 - Installing the servo software on the Arduino

The servo motor controller software is responsible for opening and closing the PEZ candy dispenser. It uses the OzServoController.h module, that is installed, when you install Ozeki 10 on your computer. Note that port 11 is used when the servo motor controller configuration is added. You might also notice, that -45 degrees is set as the startup position for the servo motor.

#include <OzIDManager.h>
#include <OzServoController.h>

OzIDManager* manager;
OzServoController* servocontroller;

void setup()
{
  Serial.begin(115200);

  manager = new OzIDManager;
  manager->_sendACK = true;
  manager->_checksum = true;

  OzCommunication::setIDManager(manager);

  servocontroller = new OzServoController;
  servocontroller->AddMotorconfig(11, ServoMotors::SERVO_SG90,-45);

  int x=1; 
  manager->sendLinkSetup();
  manager->PrintWelcomeLine(servocontroller, x++, "MyServoMotor");
  servocontroller->showMotorDetails();
}

void loop()
{
    OzCommunication::communicate();
}

Once the servo software is installed on your Arduino, your servo will be recognized by Ozeki 10. You can open the Ozeki 10 Robot Controller app and your servo will be visible in the right hand side in the controlled devices list.

Step 8 - Setting up Ozeki 10

Once the Arduino confguration is complete we are ready to finalize our robot. The robot will be controlled by Ozeki 10, a robot control software. Ozeki 10 will use the built in microphone and speaker of the PC, and it will control the servo motor through the arduino.

how the robot works
Figure 9 - How the robot works

Ozeki 10 is a messaging system for Windows. It can be used to control robots using simple text messages. If you install Ozeki 10 in your Windows laptop, it will detect all your sensors and actuators. It is a very easy to use software for robot control. To start Ozeki 10 you need to navigate to it's graphical user interface (GUI) from your webbrwoser. Enter the following URL: http://localhost:9505. After the GUI is started, you will be presented with a login form. Use admin as the login name, and the password you have supplied during installation. After successfull login you can start the robot controller app.

start ozeki ten robot controller application
Figure 10 - Start Ozeki 10 Robot Controller application

Step 9 - Installing text to speech in Ozeki 10

In the robot controller app, you need to install a text to speech connection to enable 3-CPO to read out instructions. To do this you need to select

install a text to speech connection
Figure 11 - Install a text to speech connection

Step 10 - Installing text to speech from AV

AV stands for Audio/Video in Ozeki 10. To install the text to speech connection, you need to click on AV first. Once the Audio video connection selection form opens, you can select text to speech.

select text to speech
Figure 12 - Select text to speech

Step 11 - Installing speech to text

Speech to text is similar to text to speech. The speech to text connection controlls the microphone, and when it detects an answer it converts it into text, than sends this text as a message to the robot control program. The speech to text connection can be setup the same way as text to speech.

Figure 13 - Select speech to text

Step 12 - Add words to speech to text

In order for the speech to text connection to operate correctly a set of words need to be defined. You can define individual words, or you can select a word set. Luckily there is a built in word set for detecting numbers.

define individual words or select a word set
Figure 14 - Define individual words or select a word set

Step 13 - Run the robot control code

In this robot there are three connections: a text to speech, a speech to text and a servo connection. The final step to make 3-CPO work, is to write the logic that controls the messages between these connections. The robot control logic is written in C#.NET. Once the code is ready, it can be executed using the RUN button on the toolbar.

the robot control logic
Figure 15 - The robot control logic

Step 14 - Understanding the source code

The robot source code has two important entry points: The Start and the Receive functions. When you press Run in the toolbar, the Start function is executed by the system. In this function you need to subscribe for messages coming from the text to speech and the speech to text engine.

The robot is started by sending a "Hello, please answer the question!" message to the text to speech connection. When the text to speech engine receives this message, it will read it out on the PC speaker. When it finishes, it will send a "Complete" message to the robot controller.

All incoming messages are received by the Receive function of the control code. In this function we can decide how we want to react to each message. Please read the source code to understand how the robot works:

using System;
using System.Media;
using System.Threading;
using System.Threading.Tasks;
 
namespace Ozeki
{ 
    public class Program 
    { 
        enum StateType {WaitingForQuestionToComplete, WaitingForAnswer, WaitingForResultToComplete}
        StateType State;
       
        public void Start()
        {
            SystemSounds.Beep.Play(); 
            Subscribe("My_text_to_Speech_1@localhost");
            State = StateType.WaitingForResultToComplete;
            Send("My_text_to_Speech_1@localhost","Hello, please answer the question!");
        }
       
        public void Receive(Message msg)
        {
            if(msg.FromConnection=="My_speech_to_Text_1@localhost")
            {
                if (State != StateType.WaitingForAnswer) return;
                Unsubscribe("My_speech_to_Text_1@localhost");
                evaluateResult(msg.Text);
            }  
           
            if(msg.FromConnection=="My_text_to_Speech_1@localhost" && msg.Text=="Complete")
            {
               if (State == StateType.WaitingForQuestionToComplete) 
               {
               	  Thread.Sleep(1000);
                  State = StateType.WaitingForAnswer;
                  Subscribe("My_speech_to_Text_1@localhost");
               }
               else if (State == StateType.WaitingForResultToComplete)
               {
                  askNextQuestion();
               }
            }
        }
       
        int expectedResult;
        void askNextQuestion()
        { 
            State = StateType.WaitingForQuestionToComplete;
            Random random = new Random();
            int a = random.Next(0, 10);
            int b = random.Next(0, 10);
            expectedResult = a+b;
            Send("My_text_to_Speech_1@localhost","What is "+a+" plus "+b);
        }
 
        void evaluateResult(string text)
        {
            State = StateType.WaitingForResultToComplete;
            var result = OzCommonStrings.ToInt(text);
            if (result==expectedResult) {
                Send("My_text_to_Speech_1@localhost","Very good! The answer was "+result+". "+
                    "You can take your candy.");
                dispenseCandy();
            } else {
                Send("My_text_to_Speech_1@localhost","No way! The result is not "+result+". "+
                     "The correct answer is "+expectedResult.ToString());
            }
        }
       
        void dispenseCandy()
        {
           Send("MyServoMotor@localhost","+45");
           Thread.Sleep(6000);
           Send("MyServoMotor@localhost","-45");
        }
    }
};

If you read the code, you will also notice, that 3 support functions were created, and 3 states are used to know what to do. The support function called "dispenseCandy" is called to open the 3-CPO candy dispenser using the servo. The askNextQuestion function is used to ask a question, and the evaluateResult function is used to evaluate the answer.

Step 15 - Testing the robot

Before you want to test the robot it is a good idea to load some PEZ into it. To load the candy, you need to disconnect the servo horn from the servo motor, and load the dispenser as you normally would. Once you have loaded the dispenser, you need to attach the servo horn again.

load the pez candy dispenser
Figure 16 - Load the PEZ candy dispenser

Finally, when all the candy is loaded, call your little brother over to test the system, he will love it, he will have lots of fun, while he learns a lot.

More information