<?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[Lesson 8. Servo. Analog clock]]></title><description><![CDATA[<h1>Hello! Today we will learn how to control a servo using M5Stack</h1>
<p dir="auto">Servo (from lat. servus — servant, assistant, servant), or servomechanism — mechanical transmission with automatic correction state through internal negative feedback, in accordance with the parameters set from outside. The servo has three wires: + power, - power and data wire. In order to rotate the servo to the desired angle, it is necessary to send the data wire, the pulse duration required, as shown in figure 1.</p>
<p dir="auto"><img src="https://pp.userapi.com/c830709/v830709284/510be/FjFjcaaApbg.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 1. Timelines impulse control</p>
<h2><strong>The prerequisites for lesson:</strong></h2>
<ul>
<li>
<ol>
<li>M5Stack;</li>
</ol>
</li>
<li>
<ol start="2">
<li>cable USB-C standard;</li>
</ol>
</li>
<li>
<ol start="3">
<li>colored wires from the standard set;</li>
</ol>
</li>
<li>
<ol start="4">
<li>the servo with the arrow. Model SG90;</li>
</ol>
</li>
<li>
<ol start="5">
<li>photo paper 10 x 15 cm;</li>
</ol>
</li>
<li>
<ol start="6">
<li>nail Polish;</li>
</ol>
</li>
<li>
<ol start="7">
<li>hot melt glue;</li>
</ol>
</li>
<li>
<ol start="8">
<li>Board for mounting.</li>
</ol>
</li>
</ul>
<h2>Purpose:</h2>
<p dir="auto">Make an analog clock with the hour hand. The time setting will be done with the integrated buttons and screen M5Stack. Every hour the device will make a sound.</p>
<h3>Step 1. Unpack Your servo (Fig. 1.1);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bab3/vwfKi4JW2zc.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 1.1. New kit with servo</p>
<h3>Step 2. Take only the selected components and leave the others (Fig. 2);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840230/v840230284/75558/CgHHEjD_1RI.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 2. Selection of the necessary components servo</p>
<h3>Step 3. Take the nail (Fig. 3) and paint the arrow (Fig. 4);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bac5/EZ1oJdxBUEk.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 3. Nail polish and arrow servo</p>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bace/uvFOPgFpLWk.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 4. Painting arrows</p>
<h3>Step 4. Print the photo of a dial for the watch on a normal printer (paper size 10 x 15 cm; the print file attached Download section), then make a hole with a screwdriver (Fig. 5);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bad7/0BMC_RaZq1M.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 5. Preparation of the dial</p>
<h3>Step 5. Using a glue carefully attach the dial to the body of the servo (Fig. 6);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bae0/rArBSpcXjME.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 6. Fit the dial to the body of the servo</p>
<h3>Step 6. Do the same design on a wooden platform and set the arrow (Fig. 7);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bae9/cvtI6H576D8.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 7. Installation and Assembly hours</p>
<h3>Step 7. Use colored wires for connection (Fig. 8);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4baf2/YwfV85rv9N4.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 8. Connect the servo to the device</p>
<h3>Step 8. Connect the wires to the necessary pins as shown in figure 9;</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bafb/8S3bvjg_jSQ.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 9. Wire data to the G5, wire + supply to 5V, leads - power supply to GND</p>
<h3>Step 9. Connect the device to the computer using the USB cable C (Fig. 10);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bb04/GEA8ri-CSTM.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 10. Connecting to a PC</p>
<h3>Step 10. Create a new project in the Arduino IDE and write the code:</h3>
<pre><code>#include &lt;M5Stack.h&gt;

int servoPin = 5;
// default time
int H = 0;
int M = 59;
int clockTable[12][2] = { // calibrate clock table
{28, 157}, {157, 130}, {130, 103}, {103, 79},
{76, 52}, {49, 28}, {28, 9}, {9, 130},
 {130, 103}, {103, 79}, {79, 52}, {52, 28}
};
int S = 0;
unsigned long cMls;
unsigned long pMls = 0;
bool dots = false;

int servoPulse(int angleDegrees)
{
int pulseWidth = map(angleDegrees, 0, 180, 544, 2400);
return pulseWidth;
}

void servoSet(int oldAngle, newAngle, int) {
int pulseWidth;
if (oldAngle == newAngle)
{ 
return;
}
else if (oldAngle &lt; newAngle)
{
for (int i = oldAngle; i &lt;= newAngle; i++){
pulseWidth = servoPulse(i);
digitalWrite(servoPin, HIGH);
 delayMicroseconds(pulseWidth); 
digitalWrite(servoPin, LOW);
delayMicroseconds(20000 - pulseWidth); 
}
}
else if (oldAngle &gt; newAngle)
{
for (int i = oldAngle; i &gt;= newAngle; i--){
pulseWidth = servoPulse(i);
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseWidth); 
digitalWrite(servoPin, LOW);
delayMicroseconds(20000 - pulseWidth);
}
}
}

void setH(bool readOnly = false) {
if (!readOnly) H++;
if (H &gt; 11) H = 0;
M5.Lcd.fillRect(80, 110, 70, 42, 0x00);
M5.Lcd.setTextColor(0xfbe4);
M5.Lcd.setTextSize(6);
M5.Lcd.setCursor(80, 110);
servoSet(clockTable[H][0], clockTable[H][1]);
if ((H &lt; 10) &amp;&amp; (H != 0)) M5.Lcd.print(0);
M5.Lcd.print(((H == 0) ? 12 : H));
}

void setM(bool manual = false, bool readOnly = false) {
if (!readOnly) M++;
if (M &gt; 59)
{
M = 0;
if (!manual)
{
setH();
M5.Speaker.tone(660);
delay(50);
M5.Speaker.mute();
M5.Speaker.tone(440);
delay(50);
M5.Speaker.mute();
M5.Speaker.tone(820);
delay(50);
M5.Speaker.mute();
}
}
M5.Lcd.fillRect(180, 110, 70, 42, 0x00);
M5.Lcd.setTextColor(0xfbe4);
M5.Lcd.setTextSize(6);
M5.Lcd.setCursor(180, 110);
if (M &lt; 10) M5.Lcd.print(0);
M5.Lcd.print(M);
}

void setS() {
S++;
if (S &gt; 59)
{
S = 0;
setM();
}
if (dots)
{
M5.Lcd.fillRect(150, 110, 29, 42, 0x00);
}
else
{
M5.Lcd.setTextColor(0xfbe4);
M5.Lcd.setTextSize(6);
M5.Lcd.setCursor(150, 110);
M5.Lcd.print(":");
}
dots = !dots;
}

void setup() {
m5.begin();
pinMode(servoPin, OUTPUT);
M5.Lcd.setBrightness(200);

M5.Lcd.setTextColor(0xffff);
M5.Lcd.setTextSize(3);
M5.Lcd.setCursor(65, 20);
M5.Lcd.print("Servo Clock");

M5.Lcd.setTextColor(0x7bef);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(55, 60);
M5.Lcd.print("please, setup time");

M5.Lcd.setCursor(60, 215);
M5.Lcd.printf("H");

M5.Lcd.setCursor(250, 215);
M5.Lcd.printf("M");

setH(true);
setM(true, true);
}

void loop() {
cMls = millis();
 if (M5.BtnA.wasPressed()) setH();
if (M5.BtnC.wasPressed()) setM(true);
if ((cMls - pMls) &gt; 1000)
{
pMls = cMls;
setS();
}
m5.update();
}
</code></pre>
<h3>Step 11. Upload the sketch to the device (Fig. 10.1);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c824200/v824200361/944b3/u5enVZRq4UA.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 10.1. Uploading your sketch to the device</p>
<h3>Step 12. After loading the sketch, the unit will restart and will appear on the screen menu;</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bb0d/NK9HMInw7Pw.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 11. The display menu</p>
<h3>Step 13. Set the time using buttons H and M and wait for the arrival of the next hour (Fig. 12);</h3>
<p dir="auto"><img src="https://pp.userapi.com/c840323/v840323316/4bb16/8B3mBvs7jN8.jpg" alt="" class=" img-fluid img-markdown" /><br />
Figure 12. Watch works</p>
<h2>Download</h2>
<p dir="auto">Video demonstration of the work you can download here <a href="https://yadi.sk/i/sOEpVp7r3RkPoY" target="_blank" rel="noopener noreferrer nofollow ugc">https://yadi.sk/i/sOEpVp7r3RkPoY</a></p>
<p dir="auto">The sketch can be downloaded here <a href="https://yadi.sk/d/1kpt9gH33Rm49M" target="_blank" rel="noopener noreferrer nofollow ugc">https://yadi.sk/d/1kpt9gH33Rm49M</a></p>
<p dir="auto">A file for printing of the dial can be downloaded here <a href="https://yadi.sk/d/Dr7CWpnW3Rm53V" target="_blank" rel="noopener noreferrer nofollow ugc">https://yadi.sk/d/Dr7CWpnW3Rm53V</a></p>
]]></description><link>https://community.m5stack.com/topic/77/lesson-8-servo-analog-clock</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 06:01:31 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/77.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Jan 2018 04:36:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Lesson 8. Servo. Analog clock on Thu, 01 Feb 2018 08:57:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/olivier" aria-label="Profile: olivier">@<bdi>olivier</bdi></a> thanks! in future</p>
]]></description><link>https://community.m5stack.com/post/274</link><guid isPermaLink="true">https://community.m5stack.com/post/274</guid><dc:creator><![CDATA[Dimi]]></dc:creator><pubDate>Thu, 01 Feb 2018 08:57:06 GMT</pubDate></item><item><title><![CDATA[Reply to Lesson 8. Servo. Analog clock on Tue, 30 Jan 2018 20:09:02 GMT]]></title><description><![CDATA[<p dir="auto">hi,<br />
thank you for your lesson (all lesson).<br />
Very great for me, i m beginer.</p>
<p dir="auto">Have you lesson with bluetooth</p>
<p dir="auto">thank you</p>
]]></description><link>https://community.m5stack.com/post/244</link><guid isPermaLink="true">https://community.m5stack.com/post/244</guid><dc:creator><![CDATA[olivier]]></dc:creator><pubDate>Tue, 30 Jan 2018 20:09:02 GMT</pubDate></item></channel></rss>