{"id":1064,"date":"2014-04-24T00:22:20","date_gmt":"2014-04-23T22:22:20","guid":{"rendered":"http:\/\/www.jensd.de\/wordpress\/?p=1064"},"modified":"2014-04-24T00:22:20","modified_gmt":"2014-04-23T22:22:20","slug":"between-0-and-1-pwm-with-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.jensd.de\/wordpress\/?p=1064","title":{"rendered":"Between 0 and 1 &#8211; PWM with Raspberry Pi"},"content":{"rendered":"<p>Switch on and off an LED and let run a motor at one speed and direction is easy, but what if I want to e.g. control the brightness of an LED or control motor-speed?<\/p>\n<p>A kind of analog signal is needed:<br \/>\nThis is done with <strong>Pulse-width modulation (PWM)<\/strong>. PWM is a &#8220;modulation technique that conforms the width of the pulse, formally the pulse duration, based on modulator signal information.&#8221; (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\" title=\"PWM\" target=\"_blank\">Wikipedia<\/a>).<\/p>\n<p><strong>In a nutshell<\/strong>:<br \/>\nThe average value of voltage is depending on the frequency the load is switched on and off.<\/p>\n<p><strong>Basically there are two ways to create PWM:<\/strong><\/p>\n<p><strong>Hardware PWM<\/strong><\/p>\n<ul>\n<li>very fast (max. possible frequency \/ period) <\/li>\n<li>software independent, independent from program flow<\/li>\n<li>clean signal<\/li>\n<\/ul>\n<p>BUT:<\/p>\n<ul>\n<li>not all pins might support HW PWM<\/li>\n<\/ul>\n<p><strong>Software PWM<\/strong><\/p>\n<ul>\n<li>each I\/O pin can be used for SW PWM<\/li>\n<\/ul>\n<p>BUT:<\/p>\n<ul>\n<li>software controlled timing has to be implemented<\/li>\n<li>implementation and timing have to respect CPU usage<\/li>\n<li>not a real clean signal<\/li>\n<\/ul>\n<p>In other words: HW PWM is clearly prefered!<\/p>\n<p>But unfortunately there is only one HW PWM pin (#18) available on the Rasperry Pis GPIO header (also its shared with the Audio system. That means that you can&#8217;t use PWM or Servo output and play audio through the 3.5mm jack at the same time).<\/p>\n<p>If a RasPi project needs more than 1 PWM it has to use soft-pwm.<br \/>\nHowever thank god all the hard work is almost done by using Gordon Henderson&#8217;s wiringPi-lib and its Java-API <a href=\"http:\/pi4j.com\/\" title=\"Pi4J\" target=\"_blank\">Pi4J<\/a>.<\/p>\n<p>Pi4J provides a class called &#8220;SoftPwm&#8221;. As it uses wiringPi functions, the GPIO subsytem has to be initialzed first.<br \/>\nTherefore the programm has to executed as &#8220;sudo&#8221;!<\/p>\n<p>Here is a simple example:<\/p>\n<p>[java]<br \/>\npublic class WiringPiSoftPWMExample {<\/p>\n<p>    public static void main(String[] args) throws InterruptedException {<\/p>\n<p>        \/\/ initialize wiringPi library<br \/>\n        Gpio.wiringPiSetup();<\/p>\n<p>        \/\/ create soft-pwm pins (min=0 ; max=100)<br \/>\n        SoftPwm.softPwmCreate(0, 0, 100);<\/p>\n<p>        IntegerProperty value1 = new SimpleIntegerProperty();<br \/>\n        value1.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {<br \/>\n            SoftPwm.softPwmWrite(0, newValue.intValue());<br \/>\n        });<\/p>\n<p>        \/\/ continuous loop<br \/>\n        while (true) {<br \/>\n            \/\/ fade LED to fully ON<br \/>\n            for (int i = 0; i <= 100; i++) {\n                value1.set(i);\n                Thread.sleep(25);\n            }\n\n            \/\/ fade LED to fully OFF\n            for (int i = 100; i >= 0; i&#8211;) {<br \/>\n                value1.set(i);<br \/>\n                Thread.sleep(25);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><a href=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/LED-blink.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1080\" data-permalink=\"https:\/\/www.jensd.de\/wordpress\/?attachment_id=1080\" data-orig-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/LED-blink.png\" data-orig-size=\"\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"LED blink\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/LED-blink.png\" data-large-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/LED-blink.png\" tabindex=\"0\" role=\"button\" src=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/LED-blink.png\" alt=\"LED blink\" width=\"400\" height=\"100\" class=\"aligncenter size-medium wp-image-1080\" \/><\/a><\/p>\n<p><strong>Works!<br \/>\nWhat&#8217;s next?<\/strong><\/p>\n<p><a href=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/IMG_3176.jpg\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1086\" data-permalink=\"https:\/\/www.jensd.de\/wordpress\/?attachment_id=1086\" data-orig-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/IMG_3176.jpg\" data-orig-size=\"\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"IMG_3176\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/IMG_3176.jpg\" data-large-file=\"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/IMG_3176.jpg\" tabindex=\"0\" role=\"button\" src=\"http:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/IMG_3176.jpg\" alt=\"IMG_3176\" width=\"400\" height=\"1\" class=\"aligncenter size-medium wp-image-1086\" \/><\/a><\/p>\n<p>3 x optical coupler + RGB LED stripe triggered by JavaFX UI contolled SoftPwms:<\/p>\n<p><iframe loading=\"lazy\" title=\"RGB LED stripe control\" width=\"900\" height=\"506\" src=\"https:\/\/www.youtube.com\/embed\/ExviSpsf6OM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Switch on and off an LED and let run a motor at one speed and direction is easy, but what if I want to e.g. control the brightness of an LED or control motor-speed? A kind of analog signal is needed: This is done with Pulse-width modulation (PWM). PWM is a &#8220;modulation technique that conforms&hellip; <span class=\"clear\"><\/span><a href=\"https:\/\/www.jensd.de\/wordpress\/?p=1064\" class=\"more-link read-more\" rel=\"bookmark\">Continue Reading <span class=\"screen-reader-text\">Between 0 and 1 &#8211; PWM with Raspberry Pi<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":1080,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"Between 0 and 1 - PWM with Raspberry Pi http:\/\/wp.me\/p38FCL-ha","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[21,42,30,4,18],"tags":[87,101,91,43,76,44,86],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/www.jensd.de\/wordpress\/wp-content\/uploads\/2014\/04\/LED-blink.png","jetpack_shortlink":"https:\/\/wp.me\/p38FCL-ha","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1064"}],"collection":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1064"}],"version-history":[{"count":26,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1064\/revisions"}],"predecessor-version":[{"id":1645,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1064\/revisions\/1645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/1080"}],"wp:attachment":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}