{"id":94,"date":"2013-08-02T10:39:43","date_gmt":"2013-08-02T10:39:43","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/pi\/?page_id=94"},"modified":"2013-08-27T11:46:53","modified_gmt":"2013-08-27T11:46:53","slug":"servo-motors","status":"publish","type":"page","link":"https:\/\/blog.soton.ac.uk\/pi\/servo-motors\/","title":{"rendered":"Servo Motors"},"content":{"rendered":"<p><!--\nPRE.ctl { font-family: \"Lohit Hindi\",monospace; }P { margin-bottom: 0.21cm; }A:link {  }\n--><strong>1) Introduction<\/strong><\/p>\n<p>A classical DC Motor has only two connections and, when connected to a power supply, will continuously spin. On the other hand, a servo motor operates completely different. It is formed of four parts: a DC motor, a gear reduction unit, a position-sensing device (usually a potentiometer), and a control circuit. The servo receives a control signal that represents a <em>desired output position of the servo shaft<\/em>, and applies power to its DC motor until its shaft turns to that position. The shaft typically does <em>not<\/em> rotate freely round and round like a DC motor, but rather can only turn approximately 180 degrees back and forth.<\/p>\n<p>The servo has a 3 wire connection: power, ground, and control. The power source must be constantly applied; the servo has its own drive electronics that draw current from the power lead to drive the motor.<\/p>\n<p>The servo is controlled through pulse width modulation (PWM), which means that a signal which is ON only for a certain amount of time is applied, as seen in Figure 1. The <strong>duration<\/strong> of the positive-going pulse (time in which the signal is ON) determines the <strong>position<\/strong> of the servo shaft. That means, the angle is determined by the duration of a pulse that is applied to the control wire, as seen in Figure 2. The signal has a repetition time of 20ms and the duration of the ON signal is between 1ms and 2ms. A duration of 1.5ms is usually referred to as the neutral or central position (90 degree). A longer pulse makes the servo turn to a clockwise-from-center position, and a shorter pulse makes the servo turn to a counter-clockwise-from-center position.<\/p>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Picture393LYR\">\n<p align=\"CENTER\"><a name=\"Picture393\"><\/a><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/www.servocity.com\/assets\/images\/Untitled-159.jpg\" width=\"345\" height=\"70\" align=\"BOTTOM\" border=\"0\" \/><\/p>\n<\/div>\n<p align=\"CENTER\">Figure 1: Pulse Width Modulation<\/p>\n<div dir=\"LTR\" id=\"Text40LYR\">\n<p>\u00a0\u00a0\u00a0 When these servos are commanded to move they will move to the position and hold that position. If an external force pushes against the servo while the servo is holding a position, the servo will resist from moving out of that position. Servos will not hold their position forever though; the position pulse must be repeated to instruct the servo to stay in position.<\/p>\n<\/div>\n<div dir=\"LTR\" id=\"Section1\">\n<p><a name=\"Picture393LYR\"><\/a><\/p>\n<\/div>\n<p style=\"text-align: center\"><a name=\"Picture394\"><\/a><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/www.servocity.com\/assets\/images\/Untitled-165.jpg\" width=\"392\" height=\"203\" align=\"BOTTOM\" border=\"0\" \/><\/p>\n<p style=\"text-align: center\">Figure 2: Angle of rotation of a servo, according to the pulse width<\/p>\n<p><a href=\"http:\/\/www.servocity.com\/html\/how_do_servos_work_.html\">http:\/\/www.servocity.com\/html\/how_do_servos_work_.html<\/a><\/p>\n<p><a href=\"http:\/\/handyboard.com\/hb\/faq\/hardware-faqs\/dc-vs-servo\/\">http:\/\/handyboard.com\/hb\/faq\/hardware-faqs\/dc-vs-servo\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2) Controlling a servo with Gertboard<\/strong><\/p>\n<p>The Gertboard is an add-on GPIO expansion board for the Raspberry Pi computer. It comes with a large variety of components, including buttons, LEDs, A\/D and D\/A converters, a motor controller, and an Atmel AVR microcontroller. In order to get the Gertboard powered from the Pi a jumper needs to be installed between the top two pins of the jumper J7 (located in the bottom right part of the Gertboard), as seen in Figure 3.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Power-Jumpers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-123\" alt=\"Power Jumpers\" src=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Power-Jumpers-300x146.png\" width=\"300\" height=\"146\" srcset=\"https:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Power-Jumpers-300x146.png 300w, https:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Power-Jumpers.png 666w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"text-align: center\">Figure 3: Header needed to power the Gertboard on<\/p>\n<p>&nbsp;<\/p>\n<p>The motor controller on the Gertboard can only be used to power a DC Motor, for a servo we will need to use the Atmel AVR microcontroller to produce the control signal for the servo. To do this, we will setup the Gertboard such that the microcontroller can be programmed through the <strong>Arduino IDE<\/strong>. The Gertboard can hold an Atmel AVR microcontroller, a 28-pin ATmega device, at location U8 on the lower left of the board. This is usually either the ATmega 168A\/PA or 328\/P in a 28-pin DIP package. All input\/output pins are brought out to header J25 on the left edge of the board. There is a separate 6-pin header (J23 on the left side of the board) that can be used to program the device. Note that the ATmega device on the Gertboard operates at 3.3Volts. That is in contrast to the \u2018Arduino\u2019 system which runs at 5V.<\/p>\n<p><b>Warning<\/b>: many of the Arduino example sketches (programs) mention +5V as part of the circuit. Because we are running at 3.3V, you must use 3.3V instead of 5V wherever the latter is mentioned. If you use 5V you risk damaging the chip.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Programming the ATmega <\/b><\/p>\n<p>Programming the ATmega microcontroller requires a fair bit of software to be installed on your Raspberry Pi. You can use the Arduino IDE (Integrated Development Environment) on the Raspberry Pi to develop and upload code for the ATmega chip on the Gertboard, thanks to Gordon Henderson of Drogon Systems. To install all the software needed, along with instructions, you need to go to: <a href=\"https:\/\/projects.drogon.net\/raspberry-pi\/gertboard\/arduino-ide-installation-isp\/\">https:\/\/projects.drogon.net\/raspberry-pi\/gertboard\/arduino-ide-installation-isp\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p align=\"JUSTIFY\">To check if the software has been properly installed, power up the Gertboard from the Raspberry Pi and type <b>avrsetup <\/b>in a terminal. You will be propmpted for the type of ATmega microcontroller: 1 for an ATmega 328p or 2 for the ATmega168. If this command works OK, you should see the following:<\/p>\n<pre><b>Initialising a new ATmega microcontroller for use with the Gertboard.<\/b>\r\n\r\n<b>Make sure there is a new ATmega chip plugged in, and press<\/b>\r\n<b>.. 1 for an ATmega328p or 2 for an ATmega168: 1<\/b>\r\n<b>Initialising an ATmega328p ...<\/b>\r\n<b>Looks all OK - Happy ATmega programming!<\/b><\/pre>\n<p>Once this is all done, the connections seen in Figure 4 need to be done on the Gertboard to prepare it for programming the ATmega chip.<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Atmega-programming.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-124\" alt=\"Atmega programming\" src=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Atmega-programming-300x191.png\" width=\"499\" height=\"321\" \/><\/a><\/p>\n<p style=\"text-align: center\">Figure 4: Connections to be done for programming the ATmega chip<\/p>\n<p>&nbsp;<\/p>\n<p>To get going with the ATmega chip, start up the Arduino IDE, which should be located in your start menu, under \u201cElectronics\u201d and configure it following the steps from:<strong> https:\/\/projects.drogon.net\/raspberry-pi\/gertboard\/using-the-arduino-ide<\/strong>\/. Note the differences between the arduino and Gertboard pins: <strong>https:\/\/projects.drogon.net\/raspberry-pi\/gertboard\/gertboard-atmega-io-vs-arduino-pins\/<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In order to prepare the Gertboard to control the servo, before uploading the program onto it a few more connections need to be made, as per Figure 5. The connections are:<\/p>\n<p>\u2192 PB5 on J25 is connected to B1 on J3. PB5 pin on the Gertboard is equivalent to pin 13 on Arduino, hence you will see the code works with pin 13 rather that PB5. So the pulse will be sent from PB5 to the Buffered Pin B1<\/p>\n<p>\u2192 A header attached between the two leftmost pins (labelled B1) on the connector right of C9 and below the pushbuttons S2 and S3. This will ensure that the buffer B1 is configured as an output<\/p>\n<p>\u2192 BUF1 at the top left of the Gertboard connected to the control signal (yellow wire) of the servo.<\/p>\n<p>\u2192 The GND on the Gertboard (the next right pin to BUF1) needs to go into the brown cable (GND) of the the servo<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_5219.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-169 aligncenter\" alt=\"IMG_5219\" src=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_5219-300x225.jpg\" width=\"300\" height=\"225\" srcset=\"https:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_5219-300x225.jpg 300w, https:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_5219-1024x768.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a> Figure 5: Connections to control the servo with the Gertboard<\/p>\n<p>Also you will need power from the power supply to go into the red (middle) cable of the servo and GND from the power supply into the brown cable (GND) of the servo.<\/p>\n<p>Now copy and paste the following code into Arduino IDE:<\/p>\n<p>&nbsp;<\/p>\n<p><!--\nP { margin-bottom: 0.21cm; }A:link {  }\n--><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Set the output pin<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int led = 13;http:\/\/hallshome.org\/rpiBlog\/?p=225<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">unsigned int wait = 1500; \/\/microseconds<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">unsigned int wait_MIN = 1000;\u00a0<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">unsigned int wait_MAX = 3700;\u00a0<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servo_dir = 1; \/\/ 1=up, -1 = down<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Servo speed<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeed = 20; \/\/ Current speed<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">boolean servoVariableSpeed = false;<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeedMax = 40; \/\/Max speed<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeedMin = 5; \/\/Min speed<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeedDir = 1; \/\/Current direction<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeedStep = 1; \/\/How much change the speed by<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeedTicks = 100; \/\/keeps track of ticks remaining<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int servoSpeedTicksMax = 100; \/\/max value to reset to<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int waitAtEndForTicks = 0;<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int waitTicks = 50; \/\/number of ticks to wait for at each end<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/the setup routine runs once when you press reset<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">void setup() {<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/initialize the digital pin as an output<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">pinMode(led, OUTPUT);<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">}<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/the loop routine runs over and over again forever<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">void loop() {<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Ensure the pulse duration is within an accceptable range<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if (wait &gt; wait_MAX) { wait = wait_MAX; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">else if (wait &lt; wait_MIN) {wait = wait_MIN; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0\u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Pulse the servo<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">digitalWrite(led, LOW); \/\/turn the LED on (HIGH is the voltage level)<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">delay(20); \/\/wait for 20 miliseconds<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">digitalWrite(led, HIGH); \/\/turn the LLED off by making the voltage LOW<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">delayMicroseconds(wait); \/\/wait for 1.5 seconds<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">digitalWrite(led, LOW);<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if (waitAtEndForTicks&#8211; &gt; 0) { return; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0\u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if ( (servoVariableSpeed) &amp;&amp; (servoSpeedTicks == 0) ) {<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Do we need to change direction?<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if (servoSpeed == servoSpeedMax) { servoSpeedDir = -1; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">else if (servoSpeed == servoSpeedMin) { servoSpeedDir = 1; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0\u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/ Change the speed variable<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if (servoSpeedDir == 1) { servoSpeed += servoSpeedStep; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">else { servoSpeed -= servoSpeedStep; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0\u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Reset the tick count<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">servoSpeedTicks = servoSpeedTicksMax;<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0\u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">} else { servoSpeedTicks&#8211;; } \/\/Decrement the tick<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0\u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/ if (waitAtEndForTicks&#8211; &gt;0) { return; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Unless we are waiting at the end, change the wait<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if (servo_dir == 1) { wait += servoSpeed; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">else { wait -= servoSpeed; }<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">if (wait &gt;= wait_MAX) {<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">servo_dir = -1;<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">waitAtEndForTicks = waitTicks;\u00a0<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">}<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">else if (wait &lt;= wait_MIN) {<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">servo_dir = 1;<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">waitAtEndForTicks = waitTicks;<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<span style=\"font-family: Tahoma\"><span style=\"font-size: small\">}<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\">\u00a0 \u00a0<\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">}<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Upload it onto the ATmega: <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><b>File <\/b><\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\u2192 <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><b>Upload Using Programmer. <\/b><\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Once the file uploads successfully, ensure that all the connections are in place, power the servo from a battery or from a power supply with approximately 6V (if using a power supply you will also need to adjust the current until the servo moves as programmed). Now that you have everything set up, the motor should spin approximately 180 degrees back and forth. <\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">The Gertboard can drive simultaneously as many servos as the number of buffers (12) because there are enough pins to drive 12 servos. A second servo can be controlled by connecting it to a power supply and to the Gertboard in a similar fashion to the first one. Also, a few more lines of code need to be added:<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">int led1 = 10; \/\/<\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Here we<\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"> use pin 10 which corresponds to PB2 on the Gertboard<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">pinMode(led1, OUTPUT); \/\/<\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Needs to be added inside the <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">setup() <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">routine<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"> <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Pulse the servo<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><strong> <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">digitalWrite(led<\/span><\/span><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">1<\/span><\/span><\/strong><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><strong>, LOW);<\/strong> \/\/turn the LED on (HIGH is the voltage level)<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"> <span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><strong>delay(20);<\/strong> \/\/wait for 20 miliseconds<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><strong> <span style=\"font-family: Tahoma\"><span style=\"font-size: small\">digitalWrite(led<\/span><\/span><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">1<\/span><\/span><\/strong><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><strong>, HIGH);<\/strong> \/\/turn the LLED off by making the voltage LOW<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"> <span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><strong>delayMicroseconds(wait);<\/strong> \/\/wait for 1.5 seconds<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"> <span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><strong>digitalWrite(led1, LOW);<\/strong> <\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/This section needs to be added inside the <\/span><\/span><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><b>loop() <\/b><\/span><\/span><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">routine to provide the pulse control for the second servo<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><!--\nPRE.ctl { font-family: \"Lohit Hindi\",monospace; }P { margin-bottom: 0.21cm; }A:link {  }\n--><strong><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Controlling the servo through the serial interface <\/span><\/span><\/span><\/strong><\/p>\n<p>The Arduino serial com port is not available for controlling the servo, so instead you can use an application called <strong>minicom. <\/strong>To access the serial console on the ATmega use the following procedure, by typing the commands in a terminal:<\/p>\n<pre><strong>sudo apt-get install minicom<\/strong>\r\n<strong>cd \/etc\/minicom<\/strong>\r\n<strong>sudo wget http:\/\/project-downloads.drogon.net\/gertboard\/minirc.ama0<\/strong><\/pre>\n<p>then you can simply type (in a terminal window)<\/p>\n<pre><strong>minicom ama0<\/strong><\/pre>\n<p>to open up the serial port. It\u2019s defaulted to 9600 baud, but that should be fine for most of the demo sketches.<\/p>\n<p><strong>Note<\/strong>: You may need to add the \u2018<em>pi<\/em>\u2018 user into the \u2018<em>tty<\/em>\u2018 group to be able to access the on-board serial port. If you get a permissions denied type of error, then run this command:<\/p>\n<pre><strong>sudo adduser pi tty<\/strong><\/pre>\n<p>for other Linux distributions you may need to check the permissions and ownership of the <strong>\/dev\/ttyAMA0<\/strong> device and adjust accordingly.<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">The following code will now use the <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Servo<\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"> and <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Serial <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">libraries from Arduino to control the servos, so copy and paste it into a new Arduino sketch. <\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">#include &lt;Servo.h&gt; \/\/Servo library<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Servo servoPan; \/\/ Define two objects of Servo type<\/p>\n<p>Servo servoTilt;<\/p>\n<p>&nbsp;<\/p>\n<p>void setup() \/\/Setup is only initialised once at the beginning<\/p>\n<p>{<\/p>\n<p>\/\/Attach servos to Arduino pins 9 (Gertboard PB1) and 10 (Gertboard PB2) respec$<\/p>\n<p>servoPan.attach(9);<\/p>\n<p>servoTilt.attach(10);<\/p>\n<p>&nbsp;<\/p>\n<p>Serial.begin(9600); \/\/opn a serial port at 9600baud (bits per second)<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Servos in netural position initially<\/p>\n<p>servoPan.write(90);<\/p>\n<p>servoTilt.write(90);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Loop() runs continuously<\/p>\n<p>void loop()<\/p>\n<p>{<\/p>\n<p>\/\/Send data only when you receive data<\/p>\n<p>if(Serial.available()&gt;0) {<\/p>\n<p>\/\/Read the incoming character<\/p>\n<p>char srv = Serial.read();<\/p>\n<p>\/\/Read the integer following the character<\/p>\n<p>int ang = Serial.parseInt();<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Control the left or right motor and make it move the amount set by ang<\/p>\n<p>if (srv==&#8217;l&#8217;) servoPan.write(ang);<\/p>\n<p>else if (srv==&#8217;r&#8217;) servoTilt.write(ang);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Write in the console the commands given to the motor<\/p>\n<p>Serial.print (&#8220;Motor moving: &#8220;);<\/p>\n<p>Serial.println (srv);<\/p>\n<p>Serial.print (&#8220;Position: &#8220;);<\/p>\n<p>Serial.print (ang, DEC);<\/p>\n<p>Serial.println (&#8221; degrees&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>delay(15);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Most of the connections from the previous program are made, just that this time we will not be using <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">pin 13 (PB5) <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">but <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">pin 9 (PB1).<\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"> So if you had the connections done for the previous program, move the jumper on <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">PB5 <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">to <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">PB1<\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"> (it will still connect onto Buffer 1). Also, you will need to add two jumpers, one between <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">GP14 <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">on <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">J2 <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">and <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">MCRX <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">and the second one between <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">GP15 <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">on <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">J2 <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">and <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">MCTX, <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">as shown in Figure 6 below. This connections are necessary to connect the ATmega chip\u2019s UART pins to the Raspberry Pi, in order to ensure the two can receive and transmit. MCTX and MCRX are Transmit and Receive respectively. <\/span><\/span><\/span><\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Power-Jumpers1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-128\" alt=\"Power Jumpers\" src=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/Power-Jumpers1-300x136.png\" width=\"476\" height=\"221\" \/><\/a><\/p>\n<p style=\"text-align: center\">Figure 6: Connections needed to enable serial communication (copied from Gertboard Manual)<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Now that all connections are done, <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">and ensuring that the servos are both powered <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">you can upload the code using <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">File <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\u2192<\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"> Upload Using Programmer. <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Having the serial port open <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">in a terminal <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">with minicom as described above, you can <\/span><\/span><\/span><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">now give commands to either of the two servos. Note that initially the two servos are in the neutral position (90 degrees) as initialized in <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Setup(). <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">For example, by typing <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">l60 <\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">you are commanding the motor attached to pin 9 to move at the 60degrees position. By typing <\/span><\/span><\/span><b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">r120<\/span><\/span><\/span><\/b><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"> the motor attached to pin 10 should move to the 120degrees position. <\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><!--\nP { margin-bottom: 0.21cm; }A:link {  }\n--><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><b>Controlling the Servo using light<\/b><\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Light dependent resistors (photoresistors) can be used to control a servo. Photoresistors lower their resistance with increasing light striking upon them. In the simplest configuration, the way they can be used to control a servo is explained below: <\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">First, a voltage divider is created using a photoresistor and a normal resistor. In the configuration that we will use (Figure 7), the voltage at the output (Vout) will increase with light exposure. This happens because the voltage across the photoresistor decreases (due to resistance decreasing) and this means more voltage will drop across resistor R. <\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><span style=\"color: #000000\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/www.societyofrobots.com\/images\/schematics_photoinc.jpg\" width=\"109\" height=\"149\" align=\"BOTTOM\" border=\"0\" \/> <\/span><\/p>\n<p style=\"text-align: center\"><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">(Figure 7, reference: <a href=\"http:\/\/www.societyofrobots.com\/schematics_photoresistor.shtml\">http:\/\/www.societyofrobots.com\/schematics_photoresistor.shtml<\/a>) <\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">An analog input from the Gertboard is connected to Vout such that the change in Vout will be read as a signal between 0 (meaning 0Volts) and 1023 (+3.3V for the Gertboard), according to how much light strikes across the photoresistor. This signal level will then be mapped into a value for the angle of movement of the servo (between 0 and 180 degrees) and that value is written to the servo by using a digital pin, as we did earlier. <\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Build the small circuit from here: <a href=\"http:\/\/www.instructables.com\/id\/Control-Servo-with-Light\/\">http:\/\/www.instructables.com\/id\/Control-Servo-with-Light\/<\/a> on a breadboard (replacing Digital Pin D12 with PB4 on J25 and Analog Pin A0 with PC0 on J25). Use a 10k resistor as stated on the diagram. J25 is located on the left hand side of the Gertboard. GND can be taken from J25 connector, from the pin on the right side of PC0. Also, note that the 3V3 pin from the Gertboard (Arduino 5V equivalent) top left side needs to be connected only to the photoresistor, not the servo. Now that all the connections have been done, upload the following code onto the Atmega: <\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">#include &lt;Servo.h&gt;<\/span><\/span><\/span><\/p>\n<p>\/\/Build a new object of type Servo<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Servo myservo;<br \/>\nint val;<\/span><\/span><\/span><\/p>\n<p>void setup()<br \/>\n{<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Attach the servo to pin 12 (PB4)<br \/>\nmyservo.attach(12);<br \/>\n}<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><br \/>\nvoid loop()<br \/>\n{<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Read the value of the analog pin (value between 0 and 1023)<br \/>\nval = analogRead(0);<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Map the signal value to an angle of movement for the servo<br \/>\nang = map(val, 0, 1023, 0, 179);<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/Move the servo at the angle according to the amount of light<br \/>\nmyservo.write(ang);<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">\/\/This sets the speed of movement of the servo<br \/>\ndelay(15);<br \/>\n}\u00a0<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">See how the servo moves according to the amount of light across it, by restricting the light with your hand or by using a flash-light\/lamp.<\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><b>Making the servo follow a flashlight<\/b><\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Using two photoresistors can make the servo follow the flashlight, as per the video: <a href=\"http:\/\/www.youtube.com\/watch?v=iEhX27ahreI\">http:\/\/www.youtube.com\/watch?v=iEhX27ahreI<\/a><\/span><\/span><\/span>. <span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">The small circuit has been built on a small piece of stripboard, as shown in Figure 8 below. This can be attached onto the servo such that the servo follows the flashlight quite precisely. The connections that need to be done are: pins PC1 and PC0 on the Gertboard (analog pins 1 and 0) connected to the headers as per Figure 8, digital pin PB1 on the Gertboard (pin 9) to the control signal of the servo and all the power and GND connections. Note that in Figure 8 there are three GND connections because it is easier to connect all grounds onto the same place on the stripboard. If the connections are not clear, see the diagram at http:\/\/stigern.net\/blog\/?p=103 for a more detailed diagram, where you need to replace the connections on the Arduino with the ones on the Gertboard.<br \/>\n<\/span><\/span><\/span><\/p>\n<p style=\"text-align: center\"><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><a href=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_1690.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-171\" alt=\"IMG_1690\" src=\"http:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_1690-300x225.jpg\" width=\"300\" height=\"225\" srcset=\"https:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_1690-300x225.jpg 300w, https:\/\/blog.soton.ac.uk\/pi\/files\/2013\/08\/IMG_1690-1024x768.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/span><\/span><\/span><\/p>\n<p style=\"text-align: center\">Figure 8: Stripboard with the flashlight tracker circuit<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">Then you need to upload the following code on the Atmega and you should have a working lighttracker.<\/span><\/span><\/span><\/p>\n<blockquote><p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\">#include &lt;Servo.h&gt;<\/span><\/span><\/span><\/p><\/blockquote>\n<blockquote><p>Servo myservo;<\/p><\/blockquote>\n<blockquote><p>int pos = 0;\u00a0 \/\/ Variable to store the servo position.<br \/>\nint inputPhotoLeft = 1; \/\/ Easier to read, instead of just 1 or 0.<br \/>\nint inputPhotoRight = 0;<\/p><\/blockquote>\n<blockquote><p>int Left = 0; \/\/ Store readings from the photoresistors.<br \/>\nint Right = 0; \/\/ Store readings from the photoresistors.<\/p><\/blockquote>\n<blockquote><p>void setup()<br \/>\n{<br \/>\nmyservo.attach(9); \/\/ Attach servo to pin 9.<br \/>\n}<\/p><\/blockquote>\n<blockquote><p>void loop()<br \/>\n{<br \/>\n\/\/ Reads the values from the photoresistors to the Left and Right variables.<br \/>\nLeft = analogRead(inputPhotoLeft);<br \/>\nRight = analogRead(inputPhotoRight);<\/p><\/blockquote>\n<blockquote><p>\/\/ Checks if right is greater than left, if so move to right.<br \/>\nif (Left &gt; (Right +20))<br \/>\n\/\/ +20 is the deadzone, so it wont jiggle back and forth.<br \/>\n{<br \/>\nif (pos &lt; 179)<br \/>\npos=pos+1;<br \/>\nmyservo.write(pos);<br \/>\n}<\/p><\/blockquote>\n<blockquote><p>\/\/ Checks if left is greater than right, if so move to left.<br \/>\nif (Right &gt; (Left +20))<br \/>\n\/\/ +20 is the deadzone, so it wont jiggle back and forth.<br \/>\n{<br \/>\nif (pos &gt; 1)<br \/>\npos = pos-1;<br \/>\nmyservo.write(pos);<br \/>\n}<\/p><\/blockquote>\n<blockquote><p>\/\/ Added some delay, increase or decrease if you want less or more speed.<br \/>\ndelay(10);<br \/>\n}<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000\"><span style=\"font-family: Tahoma\"><span style=\"font-size: small\"><a href=\"http:\/\/stigern.net\/blog\/?p=103\">http:\/\/stigern.net\/blog\/?p=103<\/a><\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Introduction A classical DC Motor has only two connections and, when connected to a power supply, will continuously spin. On the other hand, a servo motor operates completely different. It is formed of four parts: a DC motor, a gear reduction unit, a position-sensing device (usually a potentiometer), and a control circuit. The servo &hellip; <\/p>\n<p><a class=\"more-link block-button\" href=\"https:\/\/blog.soton.ac.uk\/pi\/servo-motors\/\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":84718,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-94","page","type-page","status-publish","hentry","nodate"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/users\/84718"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":18,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/94\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/94\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/media?parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}