<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Solved]Driving a single servo]]></title><description><![CDATA[<p dir="auto">using the M5  with firmware 1.2.3en and arduino 1.8.9</p>
<p dir="auto">I know you can't use servo.h, so I came up with a work around, or at least I thought I did.</p>
<p dir="auto">I cannot have M5.begin(); in the code or the servo doesn't work, without that the servo works but I cannot write to display.<br />
Is there a solution?  here's the example code;</p>
<p dir="auto">#include &lt;M5Stack.h&gt;</p>
<p dir="auto">#define servoPin 5<br />
int i = 0;<br />
int servoPulse(int angle) {<br />
int pulseWidth = map(angle, 0, 180, 544, 2400);<br />
return pulseWidth;<br />
}</p>
<p dir="auto">void setup() {</p>
<pre><code>m5.begin();
Serial.begin(9600);
pinMode(servoPin, OUTPUT);
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void loop() {</p>
<p dir="auto">for (int i=0; i&lt;=180; i++) {  // drive servo from 0 to 180 at max response<br />
int pulseWidth = servoPulse(i);<br />
//digitalWrite(servoPin, HIGH);<br />
delayMicroseconds(pulseWidth);<br />
//digitalWrite(servoPin, LOW);<br />
delayMicroseconds(20000 - pulseWidth);<br />
Serial.print(i);<br />
}</p>
<p dir="auto">for (int i=180; i&gt;=0; i--) {   // return to 0 at half speed<br />
int pulseWidth = servoPulse(i);<br />
//digitalWrite(servoPin, HIGH);<br />
delayMicroseconds(pulseWidth);<br />
//digitalWrite(servoPin, LOW);<br />
//    delayMicroseconds(20000 - pulseWidth);<br />
delay(50);<br />
Serial.print(i);<br />
}</p>
<p dir="auto">}</p>
]]></description><link>https://community.m5stack.com/topic/844/solved-driving-a-single-servo</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 06:21:24 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/844.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 03 Apr 2019 17:53:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solved]Driving a single servo on Sat, 13 Apr 2019 16:27:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jimw" aria-label="Profile: jimw">@<bdi>jimw</bdi></a> Please try this code.<br />
Serial speed 115200</p>
<p dir="auto">and the firmware (1.2.3-en) for micropython<br />
It is not for Arduino.</p>
<pre><code>#include &lt;M5Stack.h&gt;

int servoPin = 5;

void servoSet(int Angle, int Time) {
  int pulseWidth;
  int TimeCount;

  TimeCount = Time / 20;

  for (int i = 0; i &lt;= TimeCount; i++) {
    pulseWidth = map(Angle, 0, 180, 544, 2400);
    digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(servoPin, LOW);
    delayMicroseconds(20000 - pulseWidth);
  }
}

void setup() {
  M5.begin();
  M5.Lcd.printf("M5Stack Servo test:\n");
  pinMode(servoPin, OUTPUT);
}

void loop() {
  M5.Lcd.printf("Angle 45deg\n");
  Serial.println("Angle 45deg");
  servoSet(45, 1000); //45deg
  M5.Lcd.printf("Angle 135deg\n");
  Serial.println("Angle 135deg");
  servoSet(135, 1000); //135deg
}
</code></pre>
]]></description><link>https://community.m5stack.com/post/3720</link><guid isPermaLink="true">https://community.m5stack.com/post/3720</guid><dc:creator><![CDATA[salty_good]]></dc:creator><pubDate>Sat, 13 Apr 2019 16:27:24 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Driving a single servo on Sat, 13 Apr 2019 00:04:08 GMT]]></title><description><![CDATA[<p dir="auto">I'm using an M5stack Core with the accelerometer  built in, and arduino 1.8.9</p>
<p dir="auto">This problem occurs when using M5.begin(); which I apparently need to print to lcd inside the loop.</p>
<p dir="auto">So for instance,  inside the loop,</p>
<p dir="auto">M5.Lcd.setCursor(0, 200);<br />
M5.lcd.setTextColor(WHITE,BLACK);<br />
M5.Lcd.print (i);</p>
<p dir="auto">The Servo will not work, but instead chatter.<br />
// M5.begin();  servo works but the display does not (this statement is inside the setup, for example,</p>
<p dir="auto">void setup() {</p>
<p dir="auto">M5.begin();</p>
<p dir="auto"><a href="//myservo.attach" target="_blank" rel="noopener noreferrer nofollow ugc">//myservo.attach</a>(servoPin);</p>
<p dir="auto">}</p>
]]></description><link>https://community.m5stack.com/post/3711</link><guid isPermaLink="true">https://community.m5stack.com/post/3711</guid><dc:creator><![CDATA[JimW]]></dc:creator><pubDate>Sat, 13 Apr 2019 00:04:08 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Driving a single servo on Fri, 12 Apr 2019 19:39:03 GMT]]></title><description><![CDATA[<p dir="auto">What device are you using?<br />
I use G21 which is also SDA on port A</p>
]]></description><link>https://community.m5stack.com/post/3710</link><guid isPermaLink="true">https://community.m5stack.com/post/3710</guid><dc:creator><![CDATA[ajb2k3]]></dc:creator><pubDate>Fri, 12 Apr 2019 19:39:03 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Driving a single servo on Fri, 12 Apr 2019 19:19:19 GMT]]></title><description><![CDATA[<p dir="auto">I was hoping that it would be that simple, however, that wasn't the solution.</p>
<p dir="auto">I cannot use  M5.Lcd.print and the servo at the same time.</p>
]]></description><link>https://community.m5stack.com/post/3709</link><guid isPermaLink="true">https://community.m5stack.com/post/3709</guid><dc:creator><![CDATA[JimW]]></dc:creator><pubDate>Fri, 12 Apr 2019 19:19:19 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Driving a single servo on Tue, 09 Apr 2019 03:21:13 GMT]]></title><description><![CDATA[<p dir="auto">(Already set to serial speed 115200 in M5.begin () function)</p>
]]></description><link>https://community.m5stack.com/post/3617</link><guid isPermaLink="true">https://community.m5stack.com/post/3617</guid><dc:creator><![CDATA[salty_good]]></dc:creator><pubDate>Tue, 09 Apr 2019 03:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved]Driving a single servo on Tue, 09 Apr 2019 03:08:32 GMT]]></title><description><![CDATA[<p dir="auto">Hi, Please try to comment out this line -&gt; Serial.begin(9600);</p>
<p dir="auto">Thank you!</p>
]]></description><link>https://community.m5stack.com/post/3616</link><guid isPermaLink="true">https://community.m5stack.com/post/3616</guid><dc:creator><![CDATA[salty_good]]></dc:creator><pubDate>Tue, 09 Apr 2019 03:08:32 GMT</pubDate></item></channel></rss>