Sten-Bot Robot Kit Stensat Group LLC, Copyright 2013

Similar documents
Sten BOT Robot Kit 1 Stensat Group LLC, Copyright 2016

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

Parts List. Robotic Arm segments ¼ inch screws Cable XBEE module or Wifi module

The µbotino Microcontroller Board

Attribution Thank you to Arduino and SparkFun for open source access to reference materials.

CURIE Academy, Summer 2014 Lab 2: Computer Engineering Software Perspective Sign-Off Sheet

The Robot Builder's Shield for Arduino

The Motor sketch. One Direction ON-OFF DC Motor

INA169 Breakout Board Hookup Guide

FABO ACADEMY X ELECTRONIC DESIGN

Two Hour Robot. Lets build a Robot.

For this exercise, you will need a partner, an Arduino kit (in the plastic tub), and a laptop with the Arduino programming environment.

MICROCONTROLLERS BASIC INPUTS and OUTPUTS (I/O)

Module: Arduino as Signal Generator

Coding with Arduino to operate the prosthetic arm

Lecture 4: Basic Electronics. Lecture 4 Brief Introduction to Electronics and the Arduino

APDS-9960 RGB and Gesture Sensor Hookup Guide

MICROCONTROLLERS BASIC INPUTS and OUTPUTS (I/O)

HAW-Arduino. Sensors and Arduino F. Schubert HAW - Arduino 1

Adafruit 16-Channel Servo Driver with Arduino

Adafruit 16-channel PWM/Servo Shield

Name & SID 1 : Name & SID 2:

Introduction: Components used:

Lesson 3: Arduino. Goals

EE-110 Introduction to Engineering & Laboratory Experience Saeid Rahimi, Ph.D. Labs Introduction to Arduino

Content Components... 1 i. Acrylic Plates... 1 ii. Mechanical Fasteners... 3 iii. Electrical Components... 4 Introduction... 5 Getting Started... 6 Ar

MAKEVMA502 BASIC DIY KIT WITH ATMEGA2560 FOR ARDUINO USER MANUAL

Adafruit 16-Channel PWM/Servo HAT & Bonnet for Raspberry Pi

Tarocco Closed Loop Motor Controller

J. La Favre Using Arduino with Raspberry Pi February 7, 2018

Arduino Microcontroller Processing for Everyone!: Third Edition / Steven F. Barrett

Rodni What will yours be?

Arduino Control of Tetrix Prizm Robotics. Motors and Servos Introduction to Robotics and Engineering Marist School

1. Controlling the DC Motors

Contents. Part list 2 Preparartion 4 izebot. izebot Collision detection via Switch. izebot Serial Communication. izebot Remote Control

DFRduino Romeo All in one Controller V1.1(SKU:DFR0004)

Adafruit 16-channel PWM/Servo Shield

Arduino and Servo Motor

How to Build the Robotics++ V2 Robot. Last Edited Nov

Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi

Training Schedule. Robotic System Design using Arduino Platform

1. Introduction to Analog I/O

Lecture 6. Interfacing Digital and Analog Devices to Arduino. Intro to Arduino

CONSTRUCTION GUIDE Robotic Arm. Robobox. Level II

Lab 2: Blinkie Lab. Objectives. Materials. Theory

4WD Mobile Platform SKU:ROB0022

The Allen-Bradley Servo Interface Module (Cat. No SF1) when used with the Micro Controller (Cat. No UC1) can control single axis

Arduino: Sensors for Fun and Non Profit

A servo is an electric motor that takes in a pulse width modulated signal that controls direction and speed. A servo has three leads:

Servo click. PID: MIKROE 3133 Weight: 32 g

UNIT 4 VOCABULARY SKILLS WORK FUNCTIONS QUIZ. A detailed explanation about Arduino. What is Arduino? Listening

.:Twisting:..:Potentiometers:.

Understanding the Arduino to LabVIEW Interface

Objectives: Learn what an Arduino is and what it can do Learn what an LED is and how to use it Be able to wire and program an LED to blink

Experiment 1: Robot Moves in 3ft squared makes sound and

Mechatronics Engineering and Automation Faculty of Engineering, Ain Shams University MCT-151, Spring 2015 Lab-4: Electric Actuators

1. ASSEMBLING THE PCB 2. FLASH THE ZIP LEDs 3. BUILDING THE WHEELS

PCB & Circuit Designing (Summer Training Program) 6 Weeks/ 45 Days PRESENTED BY

LaserPING Rangefinder Module (#28041)

1 Day Robot Building (MC40A + Aluminum Base) for Edubot 2.0

In this activity, you will program the BASIC Stamp to control the rotation of each of the Parallax pre-modified servos on the Boe-Bot.

High Current DC Motor Driver Manual

EEL5666C IMDL Spring 2006 Student: Andrew Joseph. *Alarm-o-bot*

Built-in soft-start feature. Up-Slope and Down-Slope. Power-Up safe start feature. Motor will only start if pulse of 1.5ms is detected.

Demon Pumpkin APPROXIMATE TIME (EXCLUDING PREPARATION WORK): 1 HOUR PREREQUISITES: PART LIST:

Sweep / Function Generator User Guide

Embedded Controls Final Project. Tom Hall EE /07/2011

Adafruit 16-Channel Servo Driver with Arduino

C++ PROGRAM FOR DRIVING OF AN AGRICOL ROBOT

Arduino Workshop 01. AD32600 Physical Computing Prof. Fabian Winkler Fall 2014

Lab 06: Ohm s Law and Servo Motor Control

Introduction. 1 of 44

TWEAK THE ARDUINO LOGO

Arduino Lesson 1. Blink. Created by Simon Monk

B RoboClaw 2 Channel 30A Motor Controller Data Sheet

Programming 2 Servos. Learn to connect and write code to control two servos.

Mechatronics Laboratory Assignment 3 Introduction to I/O with the F28335 Motor Control Processor

Trademarks & Copyright

Studuino Icon Programming Environment Guide

Robotic Arm Assembly Instructions

EVDP610 IXDP610 Digital PWM Controller IC Evaluation Board

INSTANT ROBOT SHIELD (AXE408)

Object Detection for Collision Avoidance in ITS

Computational Crafting with Arduino. Christopher Michaud Marist School ECEP Programs, Georgia Tech

100UF CAPACITOR POTENTIOMETER SERVO MOTOR MOTOR ARM. MALE HEADER PIN (3 pins) INGREDIENTS

Battle Crab. Build Instructions. ALPHA Version

Bit:Bot The Integrated Robot for BBC Micro:Bit

Marine Debris Cleaner Phase 1 Navigation

Ocean Controls KT-5198 Dual Bidirectional DC Motor Speed Controller

Mini Hexapodinno. 18-DOF Robot

ESE 350 Microcontroller Laboratory Lab 5: Sensor-Actuator Lab

MAE106 Laboratory Exercises Lab # 1 - Laboratory tools

Bill of Materials: PWM Stepper Motor Driver PART NO

PCB & Circuit Designing (Summer Training Program 2014)

THE INPUTS ON THE ARDUINO READ VOLTAGE. ALL INPUTS NEED TO BE THOUGHT OF IN TERMS OF VOLTAGE DIFFERENTIALS.

Roborodentia Robot: Tektronix. Sean Yap Advisor: John Seng California Polytechnic State University, San Luis Obispo June 8th, 2016

Basics before Migtrating to Arduino

ZX Distance and Gesture Sensor Hookup Guide

Preface. If you have any problems for learning, please contact us at We will do our best to help you solve the problem.

Brushed DC Motor Microcontroller PWM Speed Control with Optical Encoder and H-Bridge

Transcription:

Sten-Bot Robot Kit Stensat Group LLC, Copyright 2013

Legal Stuff Stensat Group LLC assumes no responsibility and/or liability for the use of the kit and documentation. There is a 90 day warranty for the Sten-Bot kit against component defects. Damage caused by the user or owner is not covered. Warranty does not cover such things as over tightening nuts on standoffs to the point of breaking off the standoff threads, breaking wires off the motors, causing shorts to damage components, powering the motor driver backwards, plugging the power input into an AC outlet, applying more than 12 volts to the power input, dropping the kit, kicking the kit, throwing the kit in fits of rage, unforseen damage caused by the user/owner or any other method of destruction. If you do cause damage, we can sell you replacement parts or you can get most replacement parts from online hardware distributors. If you need to contact us, go to www.stensat.org and click on contact us.

Program Overview Assemble Kit Motor assembly Base plate assembly Electronics plate assembly Programming Calibration Running the Maze 3

Parts List 4 3/16 4-40 screws 1 processor board 8 1/4 4-40 screws 1 solderless bread board 8 1/2 4-40 screws 10 jumpers 4 1 4-40 screws 1 ultrasonic range sensor 24 4-30 Kep nuts 1 LED 2 1/4 standoffs 1 270 ohm resistor 4 1.5 standoffs 1 Photoresistor 4 right angle brackets 1 4.7K ohm resistor 1 metal base plate 1 micro-switch plus hardware 2 motor plates 1 USB cable 2 geared motors 1 CD 2 wheels 1 castor 1 battery holder 4

Tools Needed Philips screw driver 1/4 inch nut driver Jeweler blade screw driver 5

How the Robot Works 4 AA Batteries power the robot. The processor board executes a program to control the motors and operate the ultrasonic sensor. The motor controller supports two motors. Battery Battery Two control signals are used to control the direction and operation of each motor. A control signal causes the ultrasonic range sensor to send out a 'ping'. A signal goes back to the processor when the echo is detected. The processor times how long it takes to receive the echo to figure out distance. Processor Processor Board Board Left Left Motor Motor Right Right Motor Motor Motor Motor Controller Controller UltraUltraSonic Sonic Ranger Ranger 6

Motor Assembly The two wheels need to be mounted to the base plate. A motor mount adapter needs to be assembled. Notice that one side of the motor has a circular plastic shape sticking out. Insert ¼ inch screw as shown and secure the ¼ inch standoff. Tighten with the screw. Make sure the orientation is as in the picture. The standoff threads need to point away from the motor. 1/4 inch 7

Motor Assembly Align the motor mount adapter plate to the side of the motor. Secure the back end with 1 inch long screws Secure the top screw with a nut. For the bottom screw, install a right angle bracket. Use the larger hole that is not threaded. Secure with a nut. Secure the standoff with a nut. Install a second bracket with a ¼ inch screw and nut. Use the hole that is not threaded. 1 inch screws 1/4 inch screw 8

Motor Assembly Assemble the second motor in the mirror image of the first. The two motors should look like below. 1 x 2 1 x 2 ¼ 4-40 ¼ 4-40 9 ¼ standoffs

Mounting the Motors Align the brackets to the holes shown in the picture. Secure with two 3/16 inch screws from the bottom side. You may need to loosen the screws on the motor mount brackets to align the brackets. Re-tighten all screws. 3/16 10

Robot Plate Assembly With the motors installed, mount the caster at the rear using the two holes and the screws from the caster parts. Secure the caster holder with the two screws and nuts. 11

Electronics Base Plate Base plate is for mounting solderless bread board and processor board Solderless bread board is to be mounted in the marked rectangular area. Use the 1/2 inch screws and nuts to secure as shown in the next page. Insert the screws from the top through the solderless bread board and secure with nuts on the back side. Make sure the solderless bread board is oriented as shown in the picture on the next page. Solderless Bread Board Holes 12

Processor Board Mount The process board is mounted differently. Insert screws from the back side and install a nut on each screw. The nuts will serve as standoffs for the processor board. 13

Electronics Base Plate Assembly Place the processor board on top of the nuts. Insert another set of four nuts to secure the processor board. 14

Mounting Electronics Plate Install four 1.5 inch long standoffs with the threaded end inserted into the plate. Secure with nuts from the bottom side of the plate. 1.5 inch Standoffs 1.5 inch Standoffs 15

Mounting the Electronics Plate Secure the electronics plate on top of the standoffs using 4-40 1/4 inch screws. ¼ inch Screws ¼ inch Screws 16

Installing the Battery Holder To install the battery holder, take two pieces of double sided tape and stick them to the underside of the battery holder. Peel the other side and stick it to the base plate as shown. Using a small blade screw driver, secure the battery holder wires to the processor board. Red wire to + Side Closest to switch Black wire to right of Red 17

Installing the Wheels The wheels are installed by pressing them onto the geared motor shaft. The shaft is keyed. Press fit the wheel onto the shaft. 18

Processor Specifications Compatible to 8MHz ATMEGA168 arduino boards 8MHz operating speed 16KB Program memory 1KB Ram 512 Byte EEPROM 6 Digital IO ports 6 Analog ports Can be configured as input, output, Servo, PWM 0-5 volt range I2C Interface Built in pull up resistors Supports 100 KHz and 400 KHz speeds UART Interface Up to 57600 baud Shared with USB port USB and External power up to 12 V 500 ma max current 19

Processor Board Pinout Digital Pins D3, D5, D6, D9, D10, D11 Can be configured as Digital Pins INPUT OUTPUT Servo PWM Each digital pin has 5V and GND in a row. Allows servos to be connected directly 500ma total for all digital ports Digital 5V requires external power source Separated from USB power to Analog Pins protect PC Analog Pins 0,1,2,3,6,7 available 5V always available 0 to 5 volt input range Higher voltages will cause damage 20

Processor Board Pinout UART Uses same transmit and receive signals as USB port Use only when not plugged into USB port Supports up to 57600 baud 5V and ground included and always powered I2C 2.2 Kohm pull up resistors included 5V and ground included and always powered External power Up to 12 volts allowed Supplies power to digital port 5V pins. Up to 500ma Reverse voltage protection Over current protection Thermal overload protection Terminal block provided to allow flexibility in power source connection Power selection Move shorting block between USB and EXT UART +5V GND Transmit Receive I2C +5V GND SDA SCL Power Select Power GND +V 21

Processor Board Layout Power Switch Power Switch Switches external power on and off When USB selected, switch does not function Micro USB Port Used for programming and UART interfacing with USB host (PC) Power source selection Jumper to select between USB power and external power Set jumper to USB for power from PC Power switch is not active Digital ports do not have 5V unless external power is selected Set jumper to EXT for external power Power switch is active Digital ports have 5V 22 Micro USB Port

Arduino Software Download development software from www.arduino.cc Follow instructions on installation The processor operates at 8MHz In the arduino program select menu Tools Select Board Select Arduino Pro or Pro Mini (3.3V 8MHz) with ATMega168 Plug Processor board into the computer USB port Let the operating system find the drivers. (network connection required) Driver is also included with arduino software In the arduino program select menu Tools Select serial Port Select the appropriate COM port. Sometimes you need to guess the port Usually not COM3 which is usually the modem port 23

Using Arduino This is the arduino software It has a text editor and compiler Verify Code It uploads programs to the processor board It has a serial monitor for displaying any text sent from the arduino Upload Code New Program Open Save Serial Monitor Area for entering code error message area 24

First Program to Test Enter the program in the editor on the right. Do not copy and paste from the pdf file. It doesn't work. The compiler is case sensitive so pay attention to capitalized letters. Plug the processor board into the USB port. void setup() { Serial.begin(9600); } void loop() { Serial.println( Hello World ); } Click on the upload Code button to compile and upload the program. When the status message at the bottom of the window says done uploading, click on the serial monitor button. A window pops up with the message being displayed. Experiment by changing the message. Serial Monitor Window 25

Arduino Programming Basics The program is made up of two functions. setup() function is run at reset or power up and only once. void setup() { Serial.begin(9600); } It is used to initialize all the needed interfaces and any parameters. void loop() { Serial.println( Hello World ); delay(500); } loop() function is run after the setup() function and is repeatedly run hence the name loop. This program configures the serial interface to send messages at 9600 bits per second. The message is Hello World and is repeatedly displayed. Serial.begin() is a function that intializes the serial interface and sets the bit rate. Serial.println() sends the specified message over the serial interface and move the cursor to down one line. delay(500) is a command to stop the program for 500 milliseconds. 26

How the Solderless BreadBoard Works The solderless bread board allows circuits to be quickly connected. Each row of holes that go left to right on the top and bottom are all connected together. The columns of 5 holes are all connected together. The lines in the picture show the connections. Components and wires are inserted in the holes to make connections. Back side showing how 27 holes are connected

Using LEDs The LED is a polarized device and only works in one direction. The positive pin on the LED is the longer pin. LEDs need the current to be limited. Include a supplied 270 ohm resistor in series with the LED when connecting. 28

First Circuit The first circuit will connect the LED straight to 5 volts so the LED will always be lit when there is power. 5V The schematic for the circuit is shown to the right. The symbol at label R1 is for the resistor. LED1 is next to the symbol for the LED. The symbol at the top is the +5V connection. It is called VCC. Resistor The GND symbol is for ground. This is the zero volt reference. The LED has an anode and a cathode. The anode is the long pin. When the anode is at a higher voltage than the cathode, the LED will light. Anode Cathode 29

Wiring Diagram for LED Resistor LED Long lead 30

digitalwrite() The digitalwrite() function controls a pin and can set it high or low. When set high, the pin is set to 5 volts. When set low, the pin is set to 0 volts. The function is written as digitalwrite(pin,setting) setting is HIGH or LOW The letters need to be capital. 31

Connecting the LED to a Digital Pin Move the red wire from the 5V pin to digital pin 3. (shown on next page) The LED is not lit at this time because the digital pin 3 needs to be programmed to generate a voltage. The program to the right will cause the LED to blink. In the setup() function, digital pin 3 is configured as an output. in the loop() function, digital pin 3 is set high which causes the pin to generate 5 volts. void setup() { pinmode(3,output); } void loop() { digitalwrite(3,high); delay(500); digitalwrite(3,low); delay(500); } The delay() function halts the program for 500 milliseconds. command sets digital pin 3 to 0 volts turning off the LED. The next digitalwrite() 32

LED Connected to Port 3 Digital Pin D3 33

Photo Cell The photo cell is a light sensitive device that changes its resistance based on light intensity. The photocell can be used in a simple voltage divider circuit with another resistor. The resistor is 4.7Kohms. The photo resistor will have a resistance ranging from 1 Mohm in darkness to 100 ohms in bright light. Install the photo cell and 4.7 K resistor on the solderless bread board. Make sure the photo cell and resistor are connected. Connect the free end of the resistor to GND at the analog connector. Connect the free end of the photo cell to 5 volts. Connect the resistor and photo cell connection to pin 0 of the analog connector. 34

Photo Cell Program The program to the right will get an ADC value from analog port 0. To measure the voltage, the function analogread(port) is used. Six ports are available on the processor board. 0,1,2,3,6,7 Refer to page 5 for the location. Once the ADC value is read, it can be converted to a voltage value. The code to the right shows the equation which can be used for all the analog ports. void setup() { Serial.begin(9600); } void loop() { int a; float volts; a = analogread(0); Serial.println(a); volts = (float)a/1023.0 * 5.0; Serial.println(volts,2); delay(200); } The Serial.println function that displays the volts, includes a numeric argument which specifies the number of decimal places. 35

Motor Control Dual H-Bridge Driver is used to control the motors. It uses four transistors to control the polarity of the voltage supplied to the motor. Below shows the H-bridge driver circuit and the current flows. 36

Motor Control Controlling the motors is the same as controlling the LED except two signals are needed. With two signals, you can control the direction of the motors and turn them on and off. The following pages will describe how to hook up the motors. A motor driver module is needed. This module allows a computer to control the motors. The motors require more power than the computer signals can provide so the module provides the power. The motor driver uses what is called an H-Bridge Driver. 37

Motor Control Dual H-Bridge Driver is used to control the motors. It uses four transistors to control the polarity of the voltage supplied to the motor. The transistors are used as switches turning on and off. Below shows the H-bridge driver circuit and the current flows. + + Motor Battery - 38

Motor Control To make the motor turn on one direction, two switches need to be turned on to let power get to the motor. One switch connects the positive side of the battery to to one side of the motor and another switch connects the negative side to the other side of the motor. + + - Motor + Battery - 39

Motor Control Flip all the switches to the opposite position and the motor turns in reverse. Notice the polarity signs on the motor switched sides. + + + Motor - Battery - 40

H-Bridge Driver The motor controller module consists of two H-bridge drivers to control two motors. The circuit side is shown at the top right. The square block in the center contains the two motor drivers. The bottom picture shows the signal names next to the pins. Power is supplied at pins GND and VIN. Control signals for each motor is A1 IN, A2 IN, and B1 IN, B2 IN. The motors connect to the pins marked OUT. The other pins are not used. 41

How the H-Bridge Driver Works This drawing shows how the H-Bridge driver works. Only one is shown. There are two signals that control the direction and operation. Control logic decodes the two signals and turns on the appropriate switches to control the motor. The drawing shows the condition of AIN1 and AIN2 set to logic zero. + + AIN1=0 AIN2=0 Control Logic Motor Battery - - 42

How the H-Bridge Driver Works When AIN1 is set to logic 1, the motor drives in the forward direction. You will notice that setting AIN1 = 1, and AIN2=0 turns on two signals that turn on the two switches. + + AIN1=1 AIN2=0 Control Logic Motor Battery - - 43

How the H-Bridge Driver Works When AIN1 is set to logic 1, the motor drives in the forward direction. You will notice that setting AIN1 = 1, and AIN2=0 turns on two signals that turn on the two switches. + + AIN1=0 AIN2=1 Control Logic Motor Battery - - 44

How the H-Bridge Driver Works When you set both AIN1 and AIN2 to logic 1, you get a breaking action. This turns on the two bottom switches which shorts the motor connections together. The inductance created by the motor turning in one direction will power the motor to turn in the opposite direction. It causes the motor to slow down quickly. + + AIN1=1 AIN2=1 Control Logic Motor Battery - - 45

Mount the Motor Controller Insert the motor controller module into the solderless breadboard as shown. The rows of pins need to on either side of the gap down the center of the board. 46

Wiring The Motor Controller Use the jumper wires to connect the motor controller. First connect power Connect a wire from GND on the motor controller to GND at Signal D6 on the processor board. Connect a wire from VCC on the motor controller to 5V on the processor board. Connect A1 IN to D11 Connect A2 IN to D10 Connect B1 IN to D9 Connect B2 IN to D6 This completes the connections between the motor controller and the processor board. 47

Motor Controller Wiring Diagram 48

Wiring the Motors Now connect the motors. Connect the left motor wires to OUT B1 and B2 Connect the right motor wires to OUT A1 and A2 Don't worry if they are wired backwards. That will be corrected next. 49

Wiring Diagram for Motors Left Motor Right Motor 50

Testing the Motors To operate the motors, A1 or A2 need to be set high or low. Operation is simple if A1 and A2 are set off, the motors do not operate. If A1 is set high and A2 is low, the motors will turn one direction. If A1 is low and A2 is high, the motors will turn in the opposite direction. The same applies for B1 and B2. Enter the program on the right to turn the motors on. See which way they are turning and swap the motor pins if needed to make the motors spin forward. These pin settings will be used for forward motion. void setup() { pinmode(6,output); pinmode(9,output); pinmode(10,output); pinmode(11,output); } void loop() { digitalwrite(6,high); digitalwrite(9,low); digitalwrite(10,high); digitalwrite(11,low); delay(5000); digitalwrite(6,low); digitalwrite(10,low); delay(2000); } 51

Direction Control The digital pins D6 and D9 control the left motor. Setting D6 high and D9 low makes the left wheel spin forward. Setting D6 low and D9 high makes the left wheel spin reverse. Setting D6 low and D9 low turns off the motor. The digital pins D10 and D11 control the right motor. Setting D10 high and D11 low makes the left wheel spin forward. Setting D10 low and D11 high makes the left wheel spin reverse. Setting D10 low and D11 low turns off the motor. Making the left motor go forward and the right motor go reverse turns the robot right. Making the left motor go reverse and the right motor go forward turns the robot left. The next page shows the code for each direction. 52

Direction Control Code Forward Motion digitalwrite(6,high); digitalwrite(9,low); digitalwrite(10,high); digitalwrite(11,low); Right Turn digitalwrite(6,low); digitalwrite(9,high); digitalwrite(10,high); digitalwrite(11,low); Reverse Motion digitalwrite(6,low); digitalwrite(9,high); digitalwrite(10,low); digitalwrite(11,high); Left Turn digitalwrite(6,high); digitalwrite(9,low); digitalwrite(10,low); digitalwrite(11,high); Stop digitalwrite(6,low); digitalwrite(9,low); digitalwrite(10,low); digitalwrite(11,low); 53

Driving Around Once motor wiring has been set, modify the program to drive around. Add to the program to turn and reverse. Include a delay between setting the directions to give the robot time to move. 54

Speed Control It may be noticed that the robot may tend to drift to the left or right. This is due to the motors not being equally powerful. There is a way to attempt to equalize them by controlling their speed. A simple way to control the speed is to pulse power to the motors. This technique is called pulse width modulation. On the arduino, the analogwrite() function performs this. It generates a repeating pulse at about 250 Hz. The size of each pulse is the duty cycle. The higher the duty cycle the more power the motor gets. Adjusting the duty cycle will adjust the motor speed. 55

analogwrite() The function analogwrite() function takes two values. First is the pin number. Second is the duty cycle represented as a value from 0 to 255. 0 is 0% duty cycle. 255 is 100% duty cycle. 127 is 50% duty cycle. The function is written as analogwrite(pin,duty); 56

Controlling Motor Speed void setup() Enter the program to the right. This program { generates a PWM signal to the motor. Only one side needs a PWM signal. The other is set to 0 so no PWM signal is present. The code sets the PWM signal to 255 which is 100% duty cycle meaning it is on all the time. This is the same as digitalwrite() function. Run the code and see which direction the robot drifts. Reduce the value for the opposite direction by 10 and try again. Keep adjusting until the robot drives relatively straight. It won't be perfect. pinmode(6,output); pinmode(9,output); pinmode(10,output); pinmode(11,output); } void loop() { analogwrite(6,255); analogwrite(9,0); analogwrite(10,255); analogwrite(11,0); delay(5000); analogwrite(6,0); analogwrite(10,0); delay(2000); } 57

Calibrating Travel Distance Since there is no feedback on the motors to detect distance or wheel rotation, time will be used to specify the distance and the amount of turning. Mark off two feet on the floor. Floor tile is usually 1 foot square. Start 2 Feet Write a program to move forward two feet and stop. Start with a delay of 1000 ms. Adjust the delay until the robot travels two feet. Keep this value. Stop If necessary, adjust the PWM values to keep the robot as straight as possible. 58

Calibrating Turns Now mark on the floor a right angle. If the floor has tiles, use the corner of a tile for your right angle. Program the robot to turn right and set the delay to 400 ms and turn off. Floor Tile Place the robot on the corner of the right angle facing the left line. See how much the robot turns and adjust the delay until it turns 90 degrees. Verify the value turning left and adjust if necessary. 59

Contact Sensing There are ways for the robot to sense its environment. A simple method is to use a contact switch. The micro-switch to the right is to be mounted to the base of the robot kit. The micro-switch comes with two 5/8 long screws and nuts. Position the micro-switch as shown to the right and insert the screws from the bottom. Secure each screw with a not on the top side. Tighten until snug but do not over tighten as that may damage the switch. 60

Contact Sensing The picture shows the mounting locations on the bottoms side of the base plate. The micro-switch is installed at an angle. Take two jumper wires and insert them into the micro-switch. Connect one jumper wire to GND near digital signal D3 of the processor board. Connect the other wire to digital signal D3. D3 GND 61

Contact Sensing Software The program to the right senses the state of the switch and displays a message indicating the state. void setup() { Serial.begin(9600); pinmode(3,input_pullup); } Digital pin D3 is configured as an input with internal pull-up resistor activated. That means when nothing is connected, D3 will indicate a value 1, high signal. void loop() { int a; a = digitalread(3); Serial.println(a); delay(200); } When the micro-switch is pressed, the internal contact is closed which connected GND to D3. The program will indicate a value 0, low signal. 62

Collision Detection Program This program moves the robot forward until it bumps into something. It will then back up, turn and resume moving forward. void setup() { Serial.begin(9600); pinmode(3,input_pullup); pinmode(6,output); pinmode(9,output); pinmode(10,output); pinmode(11,output); } void loop() { int a; digitalwrite(6,high); // go forward digitalwrite(10,high); a = digitalread(3); // check contact if(a == 0) { digitalwrite(6,low); //stop digitalwrite(10,low); digitalwrite(9,high); // go reverse digitalwrite(11,high); delay(500); digitalwrite(11,low); // turn delay(400); digitalwrite(9,low); // stop } } 63

Code Explanation The first part of the loop has the robot moving forward. The micro-switch is checked to see if it closed. If the micro-switch is closed, variable a is zero. This causes this block of code to be executed. This code makes the robot stop. This code makes the robot go reverse. This code makes the robot turn right after 500ms of going reverse. void loop() { int a; digitalwrite(6,high); digitalwrite(10,high); a = digitalread(3); if(a == 0) { digitalwrite(6,low); digitalwrite(10,low); digitalwrite(9,high); digitalwrite(11,high); delay(500); digitalwrite(11,low); delay(400); digitalwrite(9,low); } } This code makes the robot stop after 400ms of turning. 64

Sensing the Environment To detect things in the environment for purpose of collision avoidance, an ultrasonic range sensor will be added to the robot. This sensor sends out a burst of audio signal at 40 Khz and detects the echo. The processor needs to measure the time it takes for the echo to return. This sensor has four pins Ground 5 Volt power input Trigger Echo 65

Mounting the Ultrasonic Ranger Insert the ultrasonic ranger as shown. It should be mounted close to the center of the robot. The pins are inserted at the end of the rows. Connect jumpers from the sensor to the processor GND to Analog GND ECHO to pin D3 TRIG to pin D5 VCC to Analog 5V Look on the processor board for the word ANALOG. The power connections are done there to isolate the sensor from the motor power to reduce electrical noise. 66

Ultrasonic Sensor The ultrasonic sensor has two signals, trigger and echo. A pulse is sent to the trigger and then the processor is to time when the echo returns. This requires two digital pins, one configured as an output and the other as an input. A new command that will be used is called pulsein(). This measures the time it takes a pulse to occur in microseconds. Try the program to the right. The results are in centimeters. void setup() { Serial.begin(9600); pinmode(3,input); pinmode(5,output); } void loop() { digitalwrite(5,low); delaymicroseconds(2); digitalwrite(5,high); delaymicroseconds(10); digitalwrite(5,low); int distance = pulsein(3,high); distance = distance/58; Serial.println(distance); delay(500); } 67

Making a Function To make this useful for other programs, this program needs to be turned into a function. A function is a subroutine or chunk of code that can be called by a name instead of the code being inserted where ever it is needed. This function will return a result. The return command specifies which variable is sent back to the calling code. int ultrasonic() { digitalwrite(5,low); delaymicroseconds(2); digitalwrite(5,high); delaymicroseconds(10); digitalwrite(5,low); int distance = pulsein(3,high); if(distance == 0) return(1000); distance = distance/58; return(distance); } The function pulsein() returns the number of microseconds. The result is then divided by 58 to calculate the distance in centimeters. 68

Conditional Programming Not it is time to use the ultrasonic sensor to do collision avoidance. The 'if' command will be used to test if the robot will collide with an object. The format for the if statement is shown to the right. Multiple statements can be inserted between the brackets and will be executed if the condition is true. To test for equals, use '==' else allows two sets of codes to be executed depending on the condition. if(a < c) { execute code here } if(a == c) { execute this code } if(a > c) { execute this code } else { otherwise execute this code } 69

Collision Avoidance Program This program will use the code used to control the motors, the ultrasonic function, and the conditional command. Put together, the program will hopefully keep the robot from bumping into anything. Enter the code on the next page. The code should be written in a single file. The code is split on the next page since it wouldn't fit in a single column. Test it and see if you need to tweak the timing for going reverse and turning. Change the code to turn a different direction. 70

Collision Avoidance Program int ultrasonic() { digitalwrite(5,low); delaymicroseconds(2); digitalwrite(5,high); delaymicroseconds(10); digitalwrite(5,low); int distance = pulsein(3,high); if(distance == 0) return(1000); distance = distance/58; return(distance); } void setup() { pinmode(3,input); pinmode(5,output); pinmode(6,output); pinmode(9,output); pinmode(10,output); pinmode(11,output); } void loop() { int distance; digitalwrite(6,high); digitalwrite(10,high); distance = ultrasonic(); if(distance < 10) { digitalwrite(6,low); digitalwrite(10,low); digitalwrite(9,high); digitalwrite(11,high); delay(1000); digitalwrite(11,low); delay(700); digitalwrite(9,low); } } 71

Obstacle Course Time Now for the fun part. Modify and expand the program to go through the obstacle course shown below. The large square represent 2 foot grids. The red rectangles represent a barrier that can be detected with the ultrasonic range sensor. Set up some barriers out of any solid material. Use the ultrasonic range sensor to avoid crashing into the barriers and turns the right direction every time a barrier is detected. Start Finish 72

Ultrasonic Sensor Specifications Operating voltage: 5V, 2ma current Induction angle: 15 degrees Detection range: 2cm 450 cm, 3mm precision Trigger signal: 10 us minimum high pulse Module sends a 8 cycle 40 Khz burst square wave 73