{"id":846,"date":"2020-10-12T15:41:56","date_gmt":"2020-10-12T14:41:56","guid":{"rendered":"http:\/\/www.igfasouza.com\/blog\/?p=846"},"modified":"2021-04-27T11:05:12","modified_gmt":"2021-04-27T10:05:12","slug":"raspberry-pi-servo-java-duke-robot","status":"publish","type":"post","link":"http:\/\/www.igfasouza.com\/blog\/raspberry-pi-servo-java-duke-robot\/","title":{"rendered":"Raspberry Pi Servo Java Duke Robot"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-raspberry.jpg\" alt=\"\" width=\"897\" height=\"515\" class=\"alignnone size-full wp-image-847\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-raspberry.jpg 897w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-raspberry-300x172.jpg 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-raspberry-768x441.jpg 768w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-raspberry-624x358.jpg 624w\" sizes=\"auto, (max-width: 897px) 100vw, 897px\" \/><\/p>\n<p><b>What&#8217;s up?<\/b><\/p>\n<p>This post is to show how I created a Java Duke Servo demo with a Raspberry Pi. This is another blog about Java on Raspberry Pi.<\/p>\n<p>Interfacing a Servo Motor with Raspberry Pi is an interesting topic as Servo Motors are the main components of a Robot and with the help of Raspberry Pi, you can have endless opportunities in Robotics.<\/p>\n<p>Because today we celebrate children&#8217;s day in Brazil, I came with the idea to show this simple robot example using a servo motor and a Raspberry Pi. And nothing better than a Java Duke servo demo to show Java on Raspberry Pi.<\/p>\n<h2>Components<\/h2>\n<ul>\n<li>SG90 Micro-servo motor<\/li>\n<li>Jumper wires (generic)<\/li>\n<li>Raspberry Pi 3b or Arduino<\/li>\n<\/ul>\n<h2>Idea<\/h2>\n<p>Using a cardboard and Duke image to create a servo demo where Duke moves his arm.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke.jpg\" alt=\"\" width=\"948\" height=\"674\" class=\"alignnone size-full wp-image-848\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke.jpg 948w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-300x213.jpg 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-768x546.jpg 768w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/duke-624x444.jpg 624w\" sizes=\"auto, (max-width: 948px) 100vw, 948px\" \/><br \/>\nI used this image to create my demo.<\/p>\n<h2>Control a Servo Motor<\/h2>\n<p>To control a Servo Motor, you need to use a technique called Pulse Width Modulation or PWM. In PWM technique, you will be sending a pulse of variable width and the position of the Servo Motor\u2019s shaft will be set by the width or length of the Pulse.<\/p>\n<p>Nice \u201c<em>Pulse Width Modulation<\/em>\u201d explanation <a href=\"https:\/\/www.electronics-tutorials.ws\/blog\/pulse-width-modulation.html\" rel=\"noopener\" target=\"_blank\">here<\/a> <\/p>\n<p>The frequency of the PWM signal is a fixed value and is dependent on the type of Servo Motor.<br \/>\nAt 50Hz i.e. a period of 20ms, the minimum pulse width is 1ms and the maximum pulse width is 2ms. Most servo motors can have a swept area of 180 degrees i.e. 90 degrees on either side of the neutral position.<\/p>\n<p>When the pulse width of the PWM Signal is 1ms, the position of the servo is the left. The Duty Cycle of this position is (1ms\/20ms) x 100 = 5%.<\/p>\n<p>Similarly, for pulse widths of 1.5ms and 2ms, the position of the servo is middle (with a duty cycle of 7.5%) and far right (with a duty cycle of 10%).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/Untitled-drawing.jpg\" alt=\"\" width=\"746\" height=\"659\" class=\"alignnone size-full wp-image-849\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/Untitled-drawing.jpg 746w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/Untitled-drawing-300x265.jpg 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/Untitled-drawing-624x551.jpg 624w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/p>\n<ul>\n<li>so for 50hz, one frequency is 20ms<\/li>\n<li>duty cycle for 0 degree = (1\/20)*100 = 5%<\/li>\n<li>duty cycle for 90 degree = (1.5\/20)*100 = 7.5%<\/li>\n<li>duty cycle for 180 degree = (2\/20)*100 = 10%<\/li>\n<\/ul>\n<p style=\"background-color: #bde5f8;color: #00529b;border: 1px solid;border-radius: 2px;margin: 10px 0px;padding: 15px 10px;\"><strong>Note<\/strong>: Be aware here that you need to test and check your servo. You might need to calibrate this number for you.<\/p>\n<h2>Schematics<\/h2>\n<p>I\u2019m using the SG90 servo motor and you can check the Datasheet <a href=\"http:\/\/www.ee.ic.ac.uk\/pcheung\/teaching\/DE1_EE\/stores\/sg90_datasheet.pdf\" rel=\"noopener\" target=\"_blank\">here<\/a>.<\/p>\n<p>The Tower Pro SG90 Servo Motor Consists of three Pins: PWM (Orange or Yellow), VCC (Red), and GND (Brown). The VCC and GND pins must be connected to +5V and GND of the power supply.<\/p>\n<p>PWM or Signal Pin of the Servo Motor must be connected to the PWM Output of the Controller (Raspberry Pi). Based on the width of the Pulses from the PWM signal, the angle of the Servo Motor\u2019s shaft will vary.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics_pi.png\" alt=\"\" width=\"812\" height=\"661\" class=\"alignnone size-full wp-image-850\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics_pi.png 812w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics_pi-300x244.png 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics_pi-768x625.png 768w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics_pi-624x508.png 624w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics.png\" alt=\"\" width=\"1360\" height=\"688\" class=\"alignnone size-full wp-image-851\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics.png 1360w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics-300x152.png 300w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics-768x389.png 768w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics-1024x518.png 1024w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/schematics-624x316.png 624w\" sizes=\"auto, (max-width: 1360px) 100vw, 1360px\" \/><\/p>\n<p>1ms pulse for 0 degree (LEFT)<br \/>\n1.5ms pulse for 90 degree (MIDDLE)<br \/>\n2ms pulse for 180 degree (RIGHT)<\/p>\n<h2>PI4J<\/h2>\n<div class=\"codecolorer-container java blackboard\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/><\/div><\/td><td><div class=\"java codecolorer\"><span class=\"kw1\">import<\/span> <span class=\"co2\">com.pi4j.io.gpio.GpioController<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.pi4j.io.gpio.GpioFactory<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.pi4j.io.gpio.GpioPinPwmOutput<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.pi4j.io.gpio.Pin<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.pi4j.io.gpio.RaspiPin<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">import<\/span> <span class=\"co2\">com.pi4j.util.CommandArgumentParser<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">public<\/span> <span class=\"kw1\">class<\/span> TestPwmServoMotor <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw1\">static<\/span> <span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a><span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> args<span class=\"br0\">&#41;<\/span> <span class=\"kw1\">throws<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+interruptedexception\"><span class=\"kw3\">InterruptedException<\/span><\/a> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; pwm<span class=\"br0\">&#40;<\/span>args<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co3\">\/**<br \/>\n&nbsp; &nbsp; &nbsp;* @param args<br \/>\n&nbsp; &nbsp; &nbsp;* @throws InterruptedException<br \/>\n&nbsp; &nbsp; &nbsp;*\/<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw1\">static<\/span> <span class=\"kw4\">void<\/span> pwm<span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string\"><span class=\"kw3\">String<\/span><\/a><span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> args<span class=\"br0\">&#41;<\/span> <span class=\"kw1\">throws<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+interruptedexception\"><span class=\"kw3\">InterruptedException<\/span><\/a><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">final<\/span> GpioController gpio <span class=\"sy0\">=<\/span> GpioFactory.<span class=\"me1\">getInstance<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Pin pin <span class=\"sy0\">=<\/span> CommandArgumentParser.<span class=\"me1\">getPin<\/span><span class=\"br0\">&#40;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; RaspiPin.<span class=\"kw1\">class<\/span>, &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; RaspiPin.<span class=\"me1\">GPIO_00<\/span>, &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; args<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; GpioPinPwmOutput pwm <span class=\"sy0\">=<\/span> gpio.<span class=\"me1\">provisionSoftPwmOutputPin<\/span><span class=\"br0\">&#40;<\/span>pin<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; pwm.<span class=\"me1\">setPwmRange<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">100<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> sleep <span class=\"sy0\">=<\/span> <span class=\"nu0\">1000<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">for<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> i <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span> <span class=\"sy0\">;<\/span>i<span class=\"sy0\">&lt;<\/span><span class=\"nu0\">10<\/span><span class=\"sy0\">;<\/span>i<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pwm.<span class=\"me1\">setPwm<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">25<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+system\"><span class=\"kw3\">System<\/span><\/a>.<span class=\"me1\">out<\/span>.<span class=\"me1\">println<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;PWM rate is: &quot;<\/span> <span class=\"sy0\">+<\/span> pwm.<span class=\"me1\">getPwm<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+thread\"><span class=\"kw3\">Thread<\/span><\/a>.<span class=\"me1\">sleep<\/span><span class=\"br0\">&#40;<\/span>sleep<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pwm.<span class=\"me1\">setPwm<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">15<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+system\"><span class=\"kw3\">System<\/span><\/a>.<span class=\"me1\">out<\/span>.<span class=\"me1\">println<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;PWM rate is: &quot;<\/span> <span class=\"sy0\">+<\/span> pwm.<span class=\"me1\">getPwm<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+thread\"><span class=\"kw3\">Thread<\/span><\/a>.<span class=\"me1\">sleep<\/span><span class=\"br0\">&#40;<\/span>sleep<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pwm.<span class=\"me1\">setPwm<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">6<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+system\"><span class=\"kw3\">System<\/span><\/a>.<span class=\"me1\">out<\/span>.<span class=\"me1\">println<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;PWM rate is: &quot;<\/span> <span class=\"sy0\">+<\/span> pwm.<span class=\"me1\">getPwm<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+thread\"><span class=\"kw3\">Thread<\/span><\/a>.<span class=\"me1\">sleep<\/span><span class=\"br0\">&#40;<\/span>sleep<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; gpio.<span class=\"me1\">shutdown<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+system\"><span class=\"kw3\">System<\/span><\/a>.<span class=\"me1\">out<\/span>.<span class=\"me1\">println<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;pwm end&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<h2>Python<\/h2>\n<div class=\"codecolorer-container python blackboard\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">import<\/span> RPi.<span class=\"me1\">GPIO<\/span> <span class=\"kw1\">as<\/span> GPIO<br \/>\n<span class=\"kw1\">import<\/span> <span class=\"kw3\">time<\/span><br \/>\n<br \/>\n<br \/>\ncontrol <span class=\"sy0\">=<\/span> <span class=\"br0\">&#91;<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">5.5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">6<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">6.5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">7<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">7.5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">8<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">8.5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">9<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">9.5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">10<\/span><span class=\"br0\">&#93;<\/span><br \/>\nservo <span class=\"sy0\">=<\/span> <span class=\"nu0\">22<\/span><br \/>\nGPIO.<span class=\"me1\">setmode<\/span><span class=\"br0\">&#40;<\/span>GPIO.<span class=\"me1\">BOARD<\/span><span class=\"br0\">&#41;<\/span><br \/>\nGPIO.<span class=\"me1\">setup<\/span><span class=\"br0\">&#40;<\/span>servo<span class=\"sy0\">,<\/span>GPIO.<span class=\"me1\">OUT<\/span><span class=\"br0\">&#41;<\/span><br \/>\np<span class=\"sy0\">=<\/span>GPIO.<span class=\"me1\">PWM<\/span><span class=\"br0\">&#40;<\/span>servo<span class=\"sy0\">,<\/span><span class=\"nu0\">50<\/span><span class=\"br0\">&#41;<\/span><br \/>\np.<span class=\"me1\">start<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">2.5<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">try<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">while<\/span> <span class=\"kw2\">True<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">for<\/span> x <span class=\"kw1\">in<\/span> <span class=\"kw2\">range<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">11<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p.<span class=\"me1\">ChangeDutyCycle<\/span><span class=\"br0\">&#40;<\/span>control<span class=\"br0\">&#91;<\/span>x<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw3\">time<\/span>.<span class=\"me1\">sleep<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0.03<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">print<\/span> x<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">for<\/span> x <span class=\"kw1\">in<\/span> <span class=\"kw2\">range<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">9<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span>-<span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p.<span class=\"me1\">ChangeDutyCycle<\/span><span class=\"br0\">&#40;<\/span>control<span class=\"br0\">&#91;<\/span>x<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw3\">time<\/span>.<span class=\"me1\">sleep<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0.03<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">print<\/span> x<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n<span class=\"kw1\">except<\/span> <span class=\"kw2\">KeyboardInterrupt<\/span>:<br \/>\n&nbsp; &nbsp; GPIO.<span class=\"me1\">cleanup<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<h2>Arduino<\/h2>\n<p>And of course, you can do this demo with an Arduino as well.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/ff8581a2-ea36-49d4-b773-13b249ddf03f_0mIWhxt1nh.png\" alt=\"\" width=\"473\" height=\"270\" class=\"alignnone size-full wp-image-857\" srcset=\"http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/ff8581a2-ea36-49d4-b773-13b249ddf03f_0mIWhxt1nh.png 473w, http:\/\/www.igfasouza.com\/blog\/wp-content\/uploads\/2020\/10\/ff8581a2-ea36-49d4-b773-13b249ddf03f_0mIWhxt1nh-300x171.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/p>\n<div class=\"codecolorer-container java blackboard\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/><\/div><\/td><td><div class=\"java codecolorer\">#include <span class=\"sy0\">&lt;<\/span>Servo.<span class=\"me1\">h<\/span><span class=\"sy0\">&gt;<\/span><br \/>\n<br \/>\n<br \/>\nServo servo<br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> setup<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; servo.<span class=\"me1\">attach<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">9<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw4\">void<\/span> loop<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; servo.<span class=\"me1\">write<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">10000<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; servo.<span class=\"me1\">write<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">90<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">10000<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; servo.<span class=\"me1\">write<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">180<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">10000<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; servo.<span class=\"me1\">write<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">90<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">10000<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; servo.<span class=\"me1\">write<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; delay<span class=\"br0\">&#40;<\/span><span class=\"nu0\">10000<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<h2>Results<\/h2>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/Bl3Dgw-4JRs\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>The next steps are to create a code that transforms text into the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Flag_semaphore\" rel=\"noopener\" target=\"_blank\">flag language<\/a><\/p>\n<h2>Links<\/h2>\n<p><a href=\"https:\/\/pi4j.com\/1.2\/index.html\" rel=\"noopener\" target=\"_blank\">https:\/\/pi4j.com\/1.2\/index.html<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/Pi4J\/pi4j\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/Pi4J\/pi4j<\/a><\/p>\n<p><a href=\"http:\/\/www.ee.ic.ac.uk\/pcheung\/teaching\/DE1_EE\/stores\/sg90_datasheet.pdf\" rel=\"noopener\" target=\"_blank\">http:\/\/www.ee.ic.ac.uk\/pcheung\/teaching\/DE1_EE\/stores\/sg90_datasheet.pdf<\/a><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\" rel=\"noopener\" target=\"_blank\">https:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation<\/a><\/p>\n<p><a href=\"https:\/\/www.electronics-tutorials.ws\/blog\/pulse-width-modulation.html\" rel=\"noopener\" target=\"_blank\">https:\/\/www.electronics-tutorials.ws\/blog\/pulse-width-modulation.html<\/a><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Flag_semaphore\" rel=\"noopener\" target=\"_blank\">https:\/\/en.wikipedia.org\/wiki\/Flag_semaphore<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What&#8217;s up? This post is to show how I created a Java Duke Servo demo with a Raspberry Pi. This is another blog about Java on Raspberry Pi. Interfacing a Servo Motor with Raspberry Pi is an interesting topic as&hellip; <a href=\"http:\/\/www.igfasouza.com\/blog\/raspberry-pi-servo-java-duke-robot\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":848,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,24],"tags":[19,17,21,18,13],"class_list":["post-846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-raspberry-pi","tag-arduino","tag-java","tag-pi4j","tag-python","tag-raspberry-pi"],"_links":{"self":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts\/846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/comments?post=846"}],"version-history":[{"count":8,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts\/846\/revisions"}],"predecessor-version":[{"id":1211,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/posts\/846\/revisions\/1211"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/media\/848"}],"wp:attachment":[{"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/media?parent=846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/categories?post=846"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.igfasouza.com\/blog\/wp-json\/wp\/v2\/tags?post=846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}