{"id":238,"date":"2026-10-02T13:11:40","date_gmt":"2026-10-02T13:11:40","guid":{"rendered":"https:\/\/el-dev.com\/en\/?p=238"},"modified":"2026-10-02T13:15:09","modified_gmt":"2026-10-02T13:15:09","slug":"servo-control-raspberry-pi","status":"publish","type":"post","link":"https:\/\/el-dev.com\/en\/articles\/servo-control-raspberry-pi\/","title":{"rendered":"Controlling a 5V servo with a Raspberry Pi"},"content":{"rendered":"<p class=\"wp-block-paragraph\">This article will discuss controlling a 5V logic servo using a Raspberry Pi microcomputer with 3.3V logic, using a level converter.<\/p> \n\n<div style=\"position:relative; height:315px;\"><div style=\"position:absolute; left:50%; transform:translate(-50%, 0);\"><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/VQRcYHmy5MY\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><\/div><\/div> \n\n\n<h3 class=\"wp-block-heading\" id=\"logic-levels-synchronize\">Logic level matching<\/h3>\n\n\n<p class=\"wp-block-paragraph\">A common problem in electrical engineering is the compatibility of devices by logic signal voltage. Many external electronic devices operate with a logic signal of 5V to 7V. That is, a logic one at the input or output of such devices has a voltage value from 5V to 7V.<\/p> \n\n<p class=\"wp-block-paragraph\"><em>Rule 1.<\/em> If the logic signal voltage is lower than specified, the device may malfunction or not work at all.<\/p> \n\n<p class=\"wp-block-paragraph\">The Raspberry Pi microcomputer includes GPIO outputs (ports) for controlling and polling external devices. GPIO ports send\/receive a logic signal no higher than 3.3V.<\/p> \n\n<p class=\"wp-block-paragraph\"><em>Rule 2.<\/em> Never connect devices operating at voltages above 3.3V directly to GPIO logic ports. If a voltage above 3.3V reaches a GPIO logic port, it can permanently destroy that port, as well as the other GPIO ports.<\/p> \n\n\n<h3 class=\"wp-block-heading\" id=\"servo-connection-scheme\">Servo connection diagram to Raspberry Pi<\/h3>\n\n\n<p class=\"wp-block-paragraph\">To control devices with logic above 3.3V, a level converter must be included in the electrical circuit. This article used a bidirectional eight-channel level converter (see Fig. 1).<\/p> \n\n<div style=\"text-align:center;\"><div class=\"image_zoom_block\" onclick=\"image_zoom_onclick('http:\/\/el-dev.com\/ru\/wp-content\/uploads\/2020\/08\/LevelShifter.jpg', 600, 423, 'Figure 1 - bidirectional level converter from 3.3V to 5V.')\" onmouseover=\"image_mouseover(1)\" onmouseout=\"image_mouseout(1)\"><div class=\"zoom_icon_container\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/el-dev.com\/ru\/wp-content\/uploads\/2020\/08\/LevelShifter.jpg\" alt=\"\" class=\"wp-image-64\" alt=\"\" class=\"wp-image-64\" width=\"400\" height=\"282\" class=\"image_zoom_block\"\/><div class=\"zoom_icon\" id=\"zoom_icon_1\"><\/div><\/div><\/div><div class=\"image_description\">Figure 1 &#8211; bidirectional level converter from 3.3V to 5V.<\/div><\/div> \n\n<p class=\"wp-block-paragraph\">The MG995 servo accepts a control signal from 4.8V to 6.6V. Therefore, we need to raise the Raspberry Pi logic level from 3.3V to a suitable value.<\/p> \n\n<p class=\"wp-block-paragraph\">Connect a constant 3.3V voltage to the VCCA port, and connect ground from the 3.3V voltage source to the GND port. Connect a constant 5V voltage and ground from the 5V voltage source to the VCCB port. Connect the 3.3V logic level from the Raspberry Pi to port A0. The converted 5V signal is obtained at the output of port B0, respectively.<\/p> \n\n<p class=\"wp-block-paragraph\">The 3.3V power for the level converter was taken from the Raspberry Pi&#8217;s 3.3V GPIO output. We also connect the 3.3V ground from the Raspberry Pi to the converter. The 5V power for the level converter was taken from an external power source (see Fig. 2).<\/p> \n\n<div style=\"text-align:center;\"><div class=\"image_zoom_block\" onclick=\"image_zoom_onclick('http:\/\/el-dev.com\/ru\/wp-content\/uploads\/2020\/08\/ConnectionLevelShifterScheme.jpg', 1024, 662, 'Figure 2 \u2013 general connection diagram of the servo to Raspberry Pi.')\" onmouseover=\"image_mouseover(2)\" onmouseout=\"image_mouseout(2)\"><div class=\"zoom_icon_container\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/el-dev.com\/ru\/wp-content\/uploads\/2020\/08\/ConnectionLevelShifterScheme.jpg\" alt=\"\" class=\"wp-image-64\" alt=\"\" class=\"wp-image-64\" width=\"400\" height=\"259\" class=\"image_zoom_block\"\/><div class=\"zoom_icon\" id=\"zoom_icon_2\"><\/div><\/div><\/div><div class=\"image_description\">Figure 2 \u2013 general connection diagram of the servo to Raspberry Pi.<\/div><\/div> \n\n\n<h3 class=\"wp-block-heading\" id=\"servo-control-script\">Servo control<\/h3>\n\n\n<p class=\"wp-block-paragraph\">To test the servo operation, a small Python script was written. The RPi.GPIO library was used to control the GPIO ports. The servo is rotated to a given angle by pulse-width modulation (PWM) by changing the DutyCycle value of the assigned GPIO port.<\/p> \n\n<p class=\"wp-block-paragraph\">Python script for testing servo operation:<\/p> \n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import RPi.GPIO as GPIO\nimport time\n\nP_SERVO = 17 # adapt to your wiring\nfPWM = 50  # Hz (not higher with software PWM)\na = 10\nb = 2\n\ndef setup():\n    global pwm\n    GPIO.setmode(GPIO.BCM)\n    GPIO.setup(P_SERVO,GPIO.OUT)\n    pwm = GPIO.PWM(P_SERVO,fPWM)\n    pwm.start(0)\n\ndef setDirection(direction):\n    duty = float(direction)\/(180\/(10)) + 2.0\n    duty = round(duty, 2)\n    pwm.ChangeDutyCycle(duty)\n    print (&quot;direction =&quot;, direction, &quot;-&gt; duty =&quot;, duty)\n    time.sleep(2) # allow to settle\n   \nprint (&quot;starting&quot;)\nsetup()\nfor direction in range(0, 120, 10):\n    setDirection(direction)\ndirection = 0    \nsetDirection(0)    \nGPIO.cleanup() \nprint (&quot;done&quot;)<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article will discuss controlling a 5V logic servo using a Raspberry Pi microcomputer with 3.3V logic, using a level converter. Logic level matching A common problem in electrical engineering is the compatibility of devices by logic signal voltage. Many external electronic devices operate with a logic signal of 5V to 7V. That is, a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,9],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-robotics"],"_links":{"self":[{"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":1,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":239,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/posts\/238\/revisions\/239"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/media\/241"}],"wp:attachment":[{"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/el-dev.com\/en\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}