<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									KY-o40 - What nobody bothers to mention! - I/O Devices - Sensors - Modules				            </title>
            <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Fri, 04 Sep 2026 19:47:29 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4420</link>
                        <pubDate>Tue, 01 Oct 2019 13:45:36 +0000</pubDate>
                        <description><![CDATA[Corrected an obvious mistake! ? #define inputSW 6 //attach UNO pin D6 to SW pin on rotary encoder// const byte interruptPin = 2; // alternative to pin 6#define ledZero 10 //attach to an LED ...]]></description>
                        <content:encoded><![CDATA[<p>Corrected an obvious mistake! ? </p><pre>#define inputSW 6 //attach UNO pin D6 to SW pin on rotary encoder<br /><span class="hljs-keyword">// const</span> <span class="hljs-keyword">byte</span> interruptPin = <span class="hljs-number">2</span>; // alternative to pin 6<br /><br />#define ledZero 10 //attach to an LED on UNO pin D10<br /><br />void setup() {<br />    pinMode (inputSW,INPUT_PULLUP);<br />    // pinMode (interruptPin,INPUT_PULLUP);<br />    // attachInterrupt(digitalPinToInterrupt(interruptPin), doSomething, FALLING); <br />}<br /><br />void loop() {<br />    int currentStateSW = digitalRead(inputSW);<br /><br />    if(currentStateSW == LOW){<br />        digitalWrite(ledZero, HIGH); // the do something part<br />    }else{<br />        digitalWrite(ledZero, LOW);<br />    }<br />}<br /><br />void doSomething(){<br />    // another do something section<br />}</pre>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4420</guid>
                    </item>
				                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4418</link>
                        <pubDate>Tue, 01 Oct 2019 09:30:15 +0000</pubDate>
                        <description><![CDATA[Many thanks to @zeferbyThe following is the bare minimum code to poll the rotary encoder switch.#define inputSW 6 //attach  UNO pin D6 to SW pin on rotary encoder// const byte interruptPin =...]]></description>
                        <content:encoded><![CDATA[<p>Many thanks to @zeferby</p><p>The following is the bare minimum code to poll the rotary encoder switch.</p><pre>#define inputSW 6 //attach  UNO pin D6 to SW pin on rotary encoder<br /><span class="hljs-keyword">// const</span> <span class="hljs-keyword">byte</span> interruptPin = <span class="hljs-number">2</span>; // alternative to pin 6<br /><br />#define ledZero 10 //attach to an LED on UNO pin D10<br /><br />void setup() {<br />  pinMode (inputSW,INPUT_PULLUP);<br />  // pinMode (interruptPin,INPUT_PULLUP);<br />  // attachInterrupt(digitalPinToInterrupt(interruptPin), doSomething, FALLING); <br />}<br /><br />void loop() {<br />  int currentStateSW = digitalRead(inputSW);<br /><br />  if(currentStateSW == LOW){<br />    digitalWrite(ledZero, HIGH); // the do something part<br />  }<br />  else{<br />  digitalWrite(ledZero, LOW);<br />  }<br />void doSomething(){<br />  // another do something section<br />  }<br />}</pre><p><br />In a large program, it would probably be advisable to attach the switch SW to an interrupt pin (2 or 3), looking for a falling signal as the alternative commented out code above shows.</p><p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4418</guid>
                    </item>
				                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4392</link>
                        <pubDate>Mon, 30 Sep 2019 15:05:20 +0000</pubDate>
                        <description><![CDATA[@zeferbyThanks for drawing my attention to this issue, but I have also read about EEPROM wear and that 10000 write/erase cycles will wear out the EEPROM registers.I am not particularly worri...]]></description>
                        <content:encoded><![CDATA[<p>@zeferby</p><p>Thanks for drawing my attention to this issue, but I have also read about EEPROM wear and that 10000 write/erase cycles will wear out the EEPROM registers.</p><p>I am not particularly worried about this as the number of changes to be made to the registers will be nowhere near this limit, and considering the limited number of years I have left on this planet. ? </p><p>And of course, if you wear out a number registers, you can change the starting address, if the number of registers needed does not exceed 512.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4392</guid>
                    </item>
				                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4390</link>
                        <pubDate>Mon, 30 Sep 2019 14:53:14 +0000</pubDate>
                        <description><![CDATA[@pugwash I see, something like an adjustable &quot;homing&quot; position.I suppose you&#039;re aware of Flash/EEPROM wear ? I have been reading on PROGMEM after looking at some library source files, which ...]]></description>
                        <content:encoded><![CDATA[<p>@pugwash I see, something like an adjustable "homing" position.</p><p>I suppose you're aware of Flash/EEPROM wear ? I have been reading on PROGMEM after looking at some library source files, which led me to look at the EEPROM capacities...what i read did not encourage me to use the Arduino's own EEPROM but rather to add an external chip (that can be replaced when wear level gets too high) for EEPROM storage.  Of course that depends on the expected frequency of write/erase operations...</p><p>While browsing i found this repo, and especially the Atmel doc linked there, that you may be interested in :</p><p><a href="https://github.com/drjoju/Arduino-wear-leveling">https://github.com/drjoju/Arduino-wear-leveling</a></p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>ZeFerby</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4390</guid>
                    </item>
				                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4388</link>
                        <pubDate>Mon, 30 Sep 2019 14:27:03 +0000</pubDate>
                        <description><![CDATA[@zeferbyHere is a modified DBW example i edited from Bill&#039;s, with encoder switch detection (implementing a &quot;Return to Zero&quot;) and &quot;high speed&quot; encoder rotation detection, with 5 leds and the ...]]></description>
                        <content:encoded><![CDATA[<p>@zeferby</p><blockquote><p>Here is a modified DBW example i edited from Bill's, with encoder switch detection (implementing a "Return to Zero") and "high speed" encoder rotation detection, with 5 leds and the encoder switch pulled high : <strong>check the #defines at the beginning of the sketch for the pin numbers !!</strong></p></blockquote><p>Thanks for sharing!</p><p>Tried it out and works well, I am sure I will glean some info from the code regarding coding the switch.</p><p>What I am trying to achieve is the following:</p><p>I wish to be able to toggle the Arduino between "learning" and "operation" modes.</p><p>The learning mode would involve moving a servo or stepper motor to a particular position with the rotary decoder, and then storing the position in the EEPROM using the momentary switch.</p><p>The operation mode involves automatically moving the servo/stepper to the stored positions, ad infinitum or until I change the stored values. </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4388</guid>
                    </item>
				                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4385</link>
                        <pubDate>Mon, 30 Sep 2019 13:36:14 +0000</pubDate>
                        <description><![CDATA[And here is the version playing with a small SG90 micro-servo (trying to attach the source file, i&#039;ll edit if it doesn&#039;t work)]]></description>
                        <content:encoded><![CDATA[<p>And here is the version playing with a small SG90 micro-servo (trying to attach the source file, i'll edit if it doesn't work) 713 </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>ZeFerby</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4385</guid>
                    </item>
				                    <item>
                        <title>RE: KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4384</link>
                        <pubDate>Mon, 30 Sep 2019 12:49:22 +0000</pubDate>
                        <description><![CDATA[Hi @pugwash, I was playing with the same toys...Here is a modified DBW example i edited from Bill&#039;s, with encoder switch detection (implementing a &quot;Return to Zero&quot;) and &quot;high speed&quot; encoder ...]]></description>
                        <content:encoded><![CDATA[<p>Hi @pugwash, I was playing with the same toys...</p><p>Here is a modified DBW example i edited from Bill's, with encoder switch detection (implementing a "Return to Zero") and "high speed" encoder rotation detection, with 5 leds and the encoder switch pulled high : <strong>check the #defines at the beginning of the sketch for the pin numbers !!</strong></p><p>I use a high speed for <em>Serial</em>, otherwise at 9600 it cannot cope with real quick shaft turning.<br />And yes, the switch is temporary and active LOW, so i reverse the digitalRead to digitalWrite to the blue LED.</p><pre>/*<br />Rotary Encoder Demo<br />rot-encode-demo-5leds.ino<br />Demonstrates operation of Rotary Encoder<br />Displays results on Serial Monitor<br />DroneBot Workshop 2019<br />https: / / dronebotworkshop.com<br /><br />Edited by ZeFerby for "high speed" encoding detection + Return-To-Zero on encoder switch click<br />2x red LEDs for "high speed" encoder pulses, 2x yellow LEDs for "low speed" encoder pulses, 1x blue LED for SW/RTZ<br />*/<br /><br />// Rotary Encoder Inputs<br />#define inputCLK 4<br />#define inputDT 5<br />#define inputSW 6<br /><br />// LED Outputs<br />#define ledCW2 8 //"high speed" red<br />#define ledCW1 9 //"low speed" yellow<br />#define ledZero 10 //Switch click-&gt;RTZ, blue<br />#define ledCCW1 11 //"low speed" yellow<br />#define ledCCW2 12 //"high speed" red<br /><br />unsigned long intervalHighSpeed = 25;<br /><br />int counter = 0; <br />int currentStateSW;<br />int currentStateCLK;<br />int previousStateCLK;<br /><br />unsigned long previousMillisSW = 0;<br />unsigned long previousMillisCW = 0;<br />unsigned long previousMillisCCW = 0;<br />unsigned long currentMillis;<br />unsigned long encDelay;<br />bool bFast = false;<br /><br />String encDir ="";<br /><br />void setup() { <br /><br />// Set encoder pins as inputs <br />pinMode (inputCLK,INPUT);<br />pinMode (inputDT,INPUT);<br />pinMode (inputSW,INPUT_PULLUP);<br /><br />// Set LED pins as outputs<br />pinMode (ledCW2,OUTPUT);<br />pinMode (ledCW1,OUTPUT);<br />pinMode (ledZero,OUTPUT);<br />pinMode (ledCCW1,OUTPUT);<br />pinMode (ledCCW2,OUTPUT);<br /><br />// Setup Serial Monitor<br />Serial.begin (57600);<br /><br />// Read the initial state of inputCLK<br />// Assign to previousStateCLK variable<br />previousStateCLK = digitalRead(inputCLK);<br /><br />}<br /><br />void loop() { <br /><br />currentMillis = millis();<br /><br />// Read the current state of inputSW<br />currentStateSW = digitalRead(inputSW);<br />digitalWrite(ledZero, !currentStateSW); //blue<br />if ( (currentStateSW == LOW) &amp;&amp; ( (previousMillisCW &gt; previousMillisSW) || (previousMillisCCW &gt; previousMillisSW) ) )<br />{<br />counter = 0;<br />digitalWrite(ledCW2, LOW); //red<br />digitalWrite(ledCW1, LOW); //yellow<br />digitalWrite(ledCCW1, LOW); //yellow<br />digitalWrite(ledCCW2, LOW); //red<br />Serial.println("SW =&gt; RTZ !");<br />previousMillisSW = currentMillis;<br />}<br /><br />// Read the current state of inputCLK<br />currentStateCLK = digitalRead(inputCLK);<br /><br />// If the previous and the current state of the inputCLK are different then a pulse has occured<br />if ((currentStateCLK == HIGH) &amp;&amp; (currentStateCLK != previousStateCLK))<br />{ <br />// Serial.print("currentStateCLK: ");<br />// Serial.println(currentStateCLK);<br /><br />// If the inputDT state is different than the inputCLK state then <br />// the encoder is rotating counterclockwise<br />if (digitalRead(inputDT) != currentStateCLK) { <br />counter --;<br />encDir ="CCW";<br />digitalWrite(ledCW2, LOW); //red<br />digitalWrite(ledCW1, LOW); //yellow<br />encDelay = currentMillis - previousMillisCCW;<br />bFast = (encDelay &lt; intervalHighSpeed);<br />digitalWrite(ledCCW1, (bFast ? LOW : HIGH)); //yellow<br />digitalWrite(ledCCW2, (bFast ? HIGH: LOW)); //red<br />previousMillisCCW = currentMillis;<br /><br />}<br />else<br />{<br />// Encoder is rotating clockwise<br />counter ++;<br />encDir ="CW";<br />encDelay = currentMillis - previousMillisCW;<br />bFast = (encDelay &lt; intervalHighSpeed);<br />digitalWrite(ledCW2, (bFast ? HIGH: LOW)); //red<br />digitalWrite(ledCW1, (bFast ? LOW : HIGH)); //yellow<br />digitalWrite(ledCCW1, LOW); //yellow<br />digitalWrite(ledCCW2, LOW); //red<br />previousMillisCW = currentMillis;<br /><br />}<br />Serial.print("Direction: ");<br />Serial.print(encDir);<br />Serial.print(" -- Delay: ");<br />Serial.print(encDelay);<br />Serial.print(bFast ? " -- QUICK " : " -- Slow ");<br />Serial.print(" -- Value: ");<br />Serial.println(counter);<br />} <br />// Update previousStateCLK with the current state<br />previousStateCLK = currentStateCLK; <br />}</pre>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>ZeFerby</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4384</guid>
                    </item>
				                    <item>
                        <title>KY-o40 - What nobody bothers to mention!</title>
                        <link>https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4379</link>
                        <pubDate>Mon, 30 Sep 2019 10:14:40 +0000</pubDate>
                        <description><![CDATA[The KY-040 has been covered by lots of people on YouTube, even by Bill.What all of them have forgotten to mention is the middle pin labelled &quot;SW&quot; and how it works. This is a momentary switch...]]></description>
                        <content:encoded><![CDATA[<p>The KY-040 has been covered by lots of people on YouTube, even by Bill.</p><p>What all of them have forgotten to mention is the middle pin labelled "SW" and how it works. This is a momentary switch that does not work as I expected it too! I expected the pin to go HIGH if the switch was pressed, silly me!</p><p>I connected this pin directly to a pin on the Arduino and changed the sketch to monitor the pin's state and got some strange results. So I attached my oscilloscope to the "SW" and when not pressed I was getting a ripple at 0V and when the switch was pressed the 0V ripple disappears. Conclusion: when the switch is not pressed then the pin is floating and when pressed the "SW" pin is providing a route to GND.</p><p>I have confirmed this by attaching an LED with a resistor to the 5V pin then to "SW" pin. Now when pressed the LED lights up. My guess is that an Arduino pin needs a pullup resistor and will be pulled LOW when the switch is pressed.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/sensors-modules/">I/O Devices - Sensors - Modules</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/sensors-modules/ky-o40-what-nobody-bothers-to-mention/#post-4379</guid>
                    </item>
							        </channel>
        </rss>
		