{"id":530,"date":"2016-05-05T21:36:33","date_gmt":"2016-05-05T21:36:33","guid":{"rendered":"https:\/\/einhugur.com\/blog\/?page_id=530"},"modified":"2016-05-29T14:49:59","modified_gmt":"2016-05-29T14:49:59","slug":"pcf8591-analog-to-digital","status":"publish","type":"page","link":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591-analog-to-digital\/","title":{"rendered":"PCF8591 Analog To Digital"},"content":{"rendered":"<h2>In this example we are going to connect to PCF8591 AD\/DA Converter.<\/h2>\n<p>Raspberry PI has no Analog inputs\u00a0or outputs, so to talk to analog devices we need to convert the signal.<\/p>\n<p><span style=\"color: #808080;\">Updated 6. May 2016 &#8211; Added R6 and R7 to get the voltage to safe levels on the I2C bus.<\/span><\/p>\n<p><img data-attachment-id=\"520\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?fit=400%2C240&amp;ssl=1\" data-orig-size=\"400,240\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1462463636&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;160&quot;,&quot;shutter_speed&quot;:&quot;0.03030303030303&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"PCF8591\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?fit=300%2C180&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?fit=400%2C240&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-520\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?resize=400%2C240&#038;ssl=1\" alt=\"PCF8591\" width=\"400\" height=\"240\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?w=400&amp;ssl=1 400w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?resize=300%2C180&amp;ssl=1 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" data-recalc-dims=\"1\" \/><\/p>\n<p><strong>Disclaimer:<\/strong><br \/>\nWe do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI.<\/p>\n<p><strong>Pieces we use in this example:<\/strong><\/p>\n<ul>\n<li>Raspberry PI 2 (or Raspberry PI 3)<\/li>\n<li>Cobbler and Cobbler cable<\/li>\n<li>Bread board<\/li>\n<li>2 k\u03a9 resistor \u00a0(x4) &#8211; You don&#8217;t need 4 if you don&#8217;t plan to read from all the inputs.<\/li>\n<li>470\u00a0\u03a9 resistor<\/li>\n<li>15 k\u03a9 resistor (x2)<\/li>\n<li>PCF8591 AD\/DA Converter Module Analog To Digital Conversion. (Those are not expensive, I paid $6.20 for 3 piece lot, so thats close to $2.07 a piece)<\/li>\n<li>WiringPI needs to be installed on the Raspberry PI -&gt;\u00a0<a href=\"http:\/\/wiringpi.com\/download-and-install\/\">See here<\/a>.<\/li>\n<li>I2CTools needs to be installed on the Raspberry PI and I2C needs to be enabled in the Raspberry PI settings.<\/li>\n<\/ul>\n<hr \/>\n<p><strong>If you are not familiar with breadboards, how to place a cobbler on the breadboard or need to know how to read the color codings on the resistors then click here bellow<\/strong><\/p>\n<p><a class=\"mb-button mb-style-glass mb-size-small mb-corners-default mb-text-style-default \" style=\"background-color: #2b7dc4;\" href=\"https:\/\/einhugur.com\/blog\/index.php\/understanding-your-breadboard-and-placement-of-raspberry-cobbler\/\">\u00a0<i class=\"fa fa-code-fork\"><\/i>\u00a0 Using breadboard<\/a> <a class=\"mb-button mb-style-glass mb-size-small mb-corners-default mb-text-style-default \" style=\"background-color: #2b7dc4;\" href=\"https:\/\/einhugur.com\/blog\/index.php\/basic-electronics\/resistor-color-codings\/\">\u00a0<i class=\"fa fa-bars\"><\/i>\u00a0 Resistor color codings<\/a><\/p>\n<hr \/>\n<p>To install the I2CTools you use the following command:<\/p>\n<blockquote><p>sudo apt-get\u00a0install i2c-tools<\/p><\/blockquote>\n<p>You need to enable\u00a0the i2c bus in the Raspberry PI settings, which you can do in the user interface if your running Jessie or if your on older system then you can do it in console.<\/p>\n<p><strong>Understanding the\u00a0PCF8591 AD\/DA Converter board:<\/strong><\/p>\n<p>This board operates at 5V, it has 4 analog inputs, and one analog output. Each analog channel is converted to 8 bit value (0 &#8211; 255).<\/p>\n<p>The board comes with the three jumpers on as can be seen on the picture bellow:<\/p>\n<p><a href=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?ssl=1\"><img data-attachment-id=\"522\" data-permalink=\"https:\/\/einhugur.net\/blog\/pcf8591withjumpers\/\" data-orig-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?fit=400%2C260&amp;ssl=1\" data-orig-size=\"400,260\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1462463678&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;125&quot;,&quot;shutter_speed&quot;:&quot;0.03030303030303&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"PCF8591WithJumpers\" data-image-description=\"\" data-medium-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?fit=300%2C195&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?fit=400%2C260&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-522\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?resize=400%2C260&#038;ssl=1\" alt=\"PCF8591WithJumpers\" width=\"400\" height=\"260\" srcset=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?w=400&amp;ssl=1 400w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591WithJumpers.jpg?resize=300%2C195&amp;ssl=1 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>On the board there is a light sensor, a temperature\u00a0sensor and potentiometer. (Why would anyone want that ? I don&#8217;t know since those are not high quality sensors). But by default the light sensor is taking analog input 0, the temperature sensor is taking analog input 1 and the\u00a0potentiometer is wired to analog input 3, leaving only analog input 2 unused. By taking off the jumpers then you can disable those sensors and get all the analog channels for your use.<\/p>\n<p><em>P4 &#8211; Taking off P4 disconnects the temperature sensor and gives you analog channel 1 for your own use.<\/em><\/p>\n<p><em>P5- Taking off P4 disconnects the light\u00a0sensor and gives you analog channel 0 for your own use.<\/em><\/p>\n<p><em>P6- Taking off P6 disconnects the 10K\u03a9 potentiometer\u00a0and gives you analog channel 3 for your own use.<\/em><\/p>\n<p>We just take them all off for our test, I don&#8217;t see good purpose for those sensor on this board anyhow, then the board will look like this with all 4 inputs enabled:<\/p>\n<p><a href=\"https:\/\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg\"><img data-attachment-id=\"520\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?fit=400%2C240&amp;ssl=1\" data-orig-size=\"400,240\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;2.2&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;iPhone 6&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1462463636&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;4.15&quot;,&quot;iso&quot;:&quot;160&quot;,&quot;shutter_speed&quot;:&quot;0.03030303030303&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}\" data-image-title=\"PCF8591\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?fit=300%2C180&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?fit=400%2C240&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-520\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?resize=400%2C240&#038;ssl=1\" alt=\"PCF8591\" width=\"400\" height=\"240\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?w=400&amp;ssl=1 400w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591.jpg?resize=300%2C180&amp;ssl=1 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>The analog inputs go from 0 to 5 V<br \/>\nThe analog output maxed for me at 3,64 V (It&#8217;s good to measure to know what your dealing with so you can account for it in your design as cheap circuits are not always as they claim to be).<\/p>\n<p>This means for the inputs then the following applies:<\/p>\n<ul>\n<li>0 V will mean 0 for read value.<\/li>\n<li>5V will mean 255 for read value.<\/li>\n<\/ul>\n<p>And for the output then the following applies:<\/p>\n<ul>\n<li>Value of zero\u00a0will mean you get 0 V out.<\/li>\n<li>Value of 255 will mean you get about 3,64 V out.<\/li>\n<\/ul>\n<p><strong>Wiring up the board:<\/strong><\/p>\n<p><a href=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?ssl=1\"><img data-attachment-id=\"545\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591-analog-to-digital\/pcf8591schemeview2\/\" data-orig-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?fit=750%2C400&amp;ssl=1\" data-orig-size=\"750,400\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"PCF8591SchemeView2\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?fit=300%2C160&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?fit=474%2C253&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-545\" src=\"https:\/\/i2.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?resize=474%2C253&#038;ssl=1\" alt=\"PCF8591SchemeView2\" width=\"474\" height=\"253\" srcset=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?w=750&amp;ssl=1 750w, https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591SchemeView2.png?resize=300%2C160&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>We use the 2 k\u03a9 resistors\u00a0to pull down the analog channels to prevent them from having fluctuating state. If you don&#8217;t plan to read all the analog channels then its enough to have pull down resistor on the channels that you want to use.<\/p>\n<p>Note also the two 15 k\u03a9 resistors which we use to\u00a0pull down the voltage to safe levels for the I2C bus.<\/p>\n<p>Then analog device can be connected to one of the red dots on the scheme above. We will just be connecting 3,3 V from PIN 1 on the Raspberry PI to one of the analog channels. (one of the red dots).<\/p>\n<p>We have put voltage\u00a0meter on the output so we can watch how to voltage changes depending on how much output we apply on the \u00a0analog output channel. We also put a LED there. The LED will not turn on on the lower voltages.<\/p>\n<p>Here is one way to put it on the breadboard. (Try to go by scheme drawing rather than breadboard drawing to avoid mistakes and to gain more understanding)<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?ssl=1\"><img data-attachment-id=\"546\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591-analog-to-digital\/pcf8591bbview2\/\" data-orig-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?fit=750%2C452&amp;ssl=1\" data-orig-size=\"750,452\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"PCF8591BBView2\" data-image-description=\"\" data-medium-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?fit=300%2C181&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?fit=474%2C286&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-546\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?resize=474%2C286&#038;ssl=1\" alt=\"PCF8591BBView2\" width=\"474\" height=\"286\" srcset=\"https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?w=750&amp;ssl=1 750w, https:\/\/i0.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/PCF8591BBView2.png?resize=300%2C181&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?ssl=1\"><img data-attachment-id=\"82\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hc-sr501-sensor\/gpio-header\/\" data-orig-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?fit=440%2C870&amp;ssl=1\" data-orig-size=\"440,870\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"GPIO Header\" data-image-description=\"\" data-medium-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?fit=152%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?fit=440%2C870&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-medium wp-image-82\" src=\"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header-152x300.png?resize=152%2C300&#038;ssl=1\" alt=\"GPIO Header\" width=\"152\" height=\"300\" srcset=\"https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?resize=152%2C300&amp;ssl=1 152w, https:\/\/i2.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2015\/09\/GPIO-Header.png?w=440&amp;ssl=1 440w\" sizes=\"(max-width: 152px) 100vw, 152px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>Blue wire we connected to 39 (GND)<br \/>\nRed to 4 (5V)<br \/>\nGreen to pin 3 marked as GPIO 2 here above (Its a special GPIO pin that is also called SDA, you cannot choose any other pin for this)<br \/>\nGray to the pin marked as GPIO 3 here above (Its a special pin that is also called SCL, you cannot choose any other pin for this).<\/p>\n<p><strong>Sanity tests in console:<\/strong><\/p>\n<p>After you have connected everything and made sure that wiringPI, i2ctools are installed\u00a0that I2C is enabled in the Raspberry PI settings, then we are ready to do sanity tests in console.<\/p>\n<p>First thing is getting the address of the I2C device, which is in this case the analog converter board.<\/p>\n<p>To do that then we type:<\/p>\n<blockquote><p>sudo i2cdetect -y 1<\/p><\/blockquote>\n<p>This should show our device at 0x48 (if you got exactly same type of device &#8211; many other devices also use 0x48)<\/p>\n<p>Test reading the channels (we always read twice since this device will in first read return last value set even if it was on different channel)<\/p>\n<p>We select channel 0, and then read it twice (ignoring the first value).<\/p>\n<blockquote><p>i2cset -y 1 0x48 0x00<br \/>\ni2cget -y 1 0x48<br \/>\ni2cget -y 1 0x48<\/p><\/blockquote>\n<p>We select channel 1, and then read it twice (ignoring the first value).<\/p>\n<blockquote>\n<blockquote><p>i2cset -y 1 0x48 0x01<br \/>\ni2cget -y 1 0x48<br \/>\ni2cget -y 1 0x48<\/p><\/blockquote>\n<\/blockquote>\n<p>We select channel 2, and then read it twice (ignoring the first value).<\/p>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote><p>i2cset -y 1 0x48 0x02<br \/>\ni2cget -y 1 0x48<br \/>\ni2cget -y 1 0x48<\/p><\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<p>We select channel 3, and then read it twice (ignoring the first value).<\/p>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote><p>i2cset -y 1 0x48 0x03<br \/>\ni2cget -y 1 0x48<br \/>\ni2cget -y 1 0x48<\/p><\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<p>Note if your reading same channel over and over again like a stream feed then you don&#8217;t need to read twice as previous value will be from same channel making it a valid value.<\/p>\n<p>Now we test writing to the analog output full voltage:<\/p>\n<blockquote><p>i2cset -y 1 0x48 0x41 0xff<\/p><\/blockquote>\n<p>Now we test writing to the analog output zero\u00a0voltage:<\/p>\n<blockquote>\n<blockquote><p>i2cset -y 1 0x48 0x41 0x0<\/p><\/blockquote>\n<\/blockquote>\n<p>Now we test writing to the analog output half\u00a0voltage:<\/p>\n<blockquote>\n<blockquote>\n<blockquote>\n<blockquote><p>i2cset -y 1 0x48 0x41 0x80<\/p><\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<\/blockquote>\n<p><strong>Putting it all in Xojo:<\/strong><\/p>\n<p>We create a module called I2C and add the following functions to it, making their scope as Protected<\/p>\n<pre lang=\"xojo\">Protected Function Setup(devID As Int32) As Int32\r\n #If TargetARM And TargetLinux Then\r\n Soft Declare Function wpWiringPiI2CSetup Lib \"libwiringPi.so\" alias \"wiringPiI2CSetup\" (devID as Integer) as Integer\r\n Return wpWiringPiI2CSetup(devID)\r\n #Endif\r\nEnd Function\r\n\r\nProtected Function ReadReg8(fd as Integer, reg as Integer) As Integer\r\n #If TargetARM And TargetLinux Then\r\n Soft Declare Function wpWiringPiI2CReadReg8 Lib \"libwiringPi.so\" alias \"wiringPiI2CReadReg8\" (fd as Integer, reg as Integer) as Integer\r\n Return wpWiringPiI2CReadReg8(fd, reg)\r\n #Endif\r\n \r\nEnd Function\r\n\r\nProtected Function WriteReg8(fd as Integer, reg As Int32, data as Integer) As Integer\r\n #If TargetARM And TargetLinux Then\r\n Soft Declare Function wpWiringPiI2CWriteReg8 Lib \"libwiringPi.so\" alias \"wiringPiI2CWriteReg8\" (fd as Integer, reg as Integer, data as Integer) As Integer\r\n Return wpWiringPiI2CWriteReg8(fd, reg, data)\r\n #Endif\r\n \r\nEnd Function\r\n<\/pre>\n<p>Then we put\u00a04 buttons on a window to read channel 0 to 3:<\/p>\n<pre lang=\"xojo\">\/\/ Button to read channel 0\r\nSub Action()\r\n Dim handle as Integer\r\n Dim value as Integer\r\n \r\n handle = I2C.Setup(&amp;h48)\r\n \r\n if handle = -1 then\r\n MsgBox \"Failed I2CSetup\"\r\n return\r\n end if\r\n \r\n if handle &lt;&gt; -1 then\r\n \/\/ I am reading twice so I dont get previous value from different channel, if your always reading just same channel in tight loop then its ok to just read once\r\n \/\/ The value in the first reading might have no meaning since the way this board works then when you ask for value then your getting value from previous state.\r\n value = I2C.ReadReg8(handle,&amp;h0)\r\n value = I2C.ReadReg8(handle,&amp;h0) \/\/ At this point we know for sure that the previous state was &amp;h0 so we get valid value\r\n \r\n MsgBox \"Raw value: \" + Str(value) + EndOfLine + \"Voltage: \" +Format(value \/ 255.0 * 5.0,\"#.##\") + \" V\"\r\n end if\r\nEnd Sub\r\n\r\n\/\/ Button to read channel 1\r\nSub Action()\r\n Dim handle as Integer\r\n Dim value as Integer\r\n \r\n handle = I2C.Setup(&amp;h48)\r\n \r\n if handle = -1 then\r\n MsgBox \"Failed I2CSetup\"\r\n return\r\n end if\r\n \r\n if handle &lt;&gt; -1 then\r\n \/\/ I am reading twice so I dont get previous value from different channel, if your always reading just same channel in tight loop then its ok to just read once\r\n \/\/ The value in the first reading might have no meaning since the way this board works then when you ask for value then your getting value from previous state.\r\n value = I2C.ReadReg8(handle,&amp;h1)\r\n value = I2C.ReadReg8(handle,&amp;h1) \/\/ At this point we know for sure that the previous state was &amp;h1 so we get valid value\r\n \r\n MsgBox \"Raw value: \" + Str(value) + EndOfLine + \"Voltage: \" +Format(value \/ 255.0 * 5.0,\"#.##\") + \" V\"\r\n end if\r\nEnd Sub\r\n\r\n\/\/ Button to read channel 2\r\nSub Action()\r\n Dim handle as Integer\r\n Dim value as Integer\r\n \r\n handle = I2C.Setup(&amp;h48)\r\n \r\n if handle = -1 then\r\n MsgBox \"Failed I2CSetup\"\r\n return\r\n end if\r\n \r\n if handle &lt;&gt; -1 then\r\n \/\/ I am reading twice so I dont get previous value from different channel, if your always reading just same channel in tight loop then its ok to just read once\r\n \/\/ The value in the first reading might have no meaning since the way this board works then when you ask for value then your getting value from previous state.\r\n value = I2C.ReadReg8(handle,&amp;h2)\r\n value = I2C.ReadReg8(handle,&amp;h2) \/\/ At this point we know for sure that the previous state was &amp;h2 so we get valid value\r\n \r\n MsgBox \"Raw value: \" + Str(value) + EndOfLine + \"Voltage: \" +Format(value \/ 255.0 * 5.0,\"#.##\") + \" V\"\r\n end if\r\nEnd Sub\r\n\r\n\/\/ Button to read channel 3\r\n Sub Action()\r\n Dim handle as Integer\r\n Dim value as Integer\r\n \r\n handle = I2C.Setup(&amp;h48)\r\n \r\n if handle = -1 then\r\n MsgBox \"Failed I2CSetup\"\r\n return\r\n end if\r\n \r\n if handle &lt;&gt; -1 then\r\n \/\/ I am reading twice so I dont get previous value from different channel, if your always reading just same channel in tight loop then its ok to just read once\r\n \/\/ The value in the first reading might have no meaning since the way this board works then when you ask for value then your getting value from previous state.\r\n value = I2C.ReadReg8(handle,&amp;h3)\r\n value = I2C.ReadReg8(handle,&amp;h3) \/\/ At this point we know for sure that the previous state was &amp;h3 so we get valid value\r\n \r\n MsgBox \"Raw value: \" + Str(value) + EndOfLine + \"Voltage: \" +Format(value \/ 255.0 * 5.0,\"#.##\") + \" V\"\r\n end if\r\nEnd Sub\r\n<\/pre>\n<p>Then we put slider on the window, setting its initial value to 0, and min as 0 and max as 255. Here is Open event and ValueChanged events for the slider:<\/p>\n<pre lang=\"xojo\">Sub Open()\r\n Dim handle as Integer\r\n \r\n handle = I2C.Setup(&amp;h48)\r\n \r\n if handle = -1 then\r\n MsgBox \"Failed I2CSetup\"\r\n return\r\n end if\r\n \r\n if handle &lt;&gt; -1 then\r\n call I2C.WriteReg8(handle,&amp;h41,0)\r\n end if\r\nEnd Sub\r\n\r\nSub ValueChanged()\r\n Dim handle as Integer\r\n \r\n handle = I2C.Setup(&amp;h48)\r\n \r\n if handle = -1 then\r\n MsgBox \"Failed I2CSetup\"\r\n return\r\n end if\r\n \r\n if handle &lt;&gt; -1 then\r\n call I2C.WriteReg8(handle,&amp;h41,me.Value)\r\n end if\r\nEnd Sub\r\n<\/pre>\n<hr \/>\n<p><strong>Downloading the code:<\/strong><\/p>\n<p><a class=\"mb-button mb-style-glass mb-size-small mb-corners-default mb-text-style-default \" style=\"background-color: #2b7dc4;\" href=\"http:\/\/www.einhugur.com\/Downloads\/GPIO\/I2C.zip\">\u00a0<i class=\"fa fa-download\"><\/i>\u00a0 Download the code<\/a><\/p>\n<hr \/>\n<p>When testing then you can test connecting 3,3 V to the analog inputs and see how it changes.<\/p>\n<p>Having mastered this will enable usage of many analog sensors and devices.<\/p>\n<hr \/>\n<p>Note that a better way to hook up the PCF8591\u00a0is to use I2C logic converter than to use the 15k\u03a9 resistors, like for example:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?ssl=1\"><img data-attachment-id=\"696\" data-permalink=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591-analog-to-digital\/withlogicconverter\/\" data-orig-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?fit=493%2C147&amp;ssl=1\" data-orig-size=\"493,147\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"WithLogicConverter\" data-image-description=\"\" data-medium-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?fit=300%2C89&amp;ssl=1\" data-large-file=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?fit=474%2C141&amp;ssl=1\" loading=\"lazy\" class=\"alignnone size-full wp-image-696\" src=\"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?resize=474%2C141&#038;ssl=1\" alt=\"WithLogicConverter\" width=\"474\" height=\"141\" srcset=\"https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?w=493&amp;ssl=1 493w, https:\/\/i1.wp.com\/einhugur.net\/blog\/wp-content\/uploads\/2016\/05\/WithLogicConverter.png?resize=300%2C89&amp;ssl=1 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>You can see more about that in our\u00a0<a href=\"https:\/\/einhugur.com\/blog\/index.php\/xojo-gpio\/i2c-logic-level-converter-building-our-own\/\">Building I2C logic converter guide.<\/a><\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/s.click.aliexpress.com\/e\/3JmyzZR7Y\" target=\"_parent\"><img src=\"https:\/\/i2.wp.com\/g03.a.alicdn.com\/kf\/HTB1_wanJFXXXXc5aXXXq6xXFXXXK\/Multimetro-HoldPeak-HP-770D-Auto-Range-font-b-Digital-b-font-font-b-Multimeter-b-font.jpg_220x220.jpg?w=474\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product &#8211; HoldPeak HP-770D Multimeter<\/span><\/a><\/p>\n<p><a href=\"http:\/\/s.click.aliexpress.com\/e\/UB66yNRbY\" target=\"_parent\"><img src=\"https:\/\/i2.wp.com\/g03.a.alicdn.com\/kf\/HTB1uYtsIXXXXXa7XFXXq6xXFXXXY\/1-4W-0-25W-122-kind-20-PCs-2440-PCs-0-33ohm-4-7m-5-carbon.jpg_220x220.jpg?w=474\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product &#8211; 1\/4W 0.25W 122 kind * 20 PCs =2440 PCs (0.33ohm-4.7m) 5% carbon film resistors assorted kit<\/span><\/a><\/p>\n<p><a href=\"http:\/\/s.click.aliexpress.com\/e\/zBYNrVz72\" target=\"_parent\"><img src=\"https:\/\/i2.wp.com\/g03.a.alicdn.com\/kf\/HTB13t44LpXXXXauXFXXq6xXFXXXp\/-font-b-PCF8591-b-font-module-AD-DA-converter-module-font-b-analog-b-font.jpg_220x220.jpg?w=474\" data-recalc-dims=\"1\" \/><span style=\"display: block;\">AliExpress.com Product &#8211; PCF8591 module AD \/ DA converter module analog to digital \/ digital to analog conversion<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example we are going to connect to PCF8591 AD\/DA Converter. Raspberry PI has no Analog inputs\u00a0or outputs, so to talk to analog devices we need to convert the signal. Updated 6. May 2016 &#8211; Added R6 and R7 to get the voltage to safe levels on the I2C bus. Disclaimer: We do not &hellip; <a href=\"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591-analog-to-digital\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PCF8591 Analog To Digital<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":94,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"spay_email":""},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Pd75nk-8y","jetpack-related-posts":[{"id":551,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/pcf8591-analog-to-digital-braking-out-of-the-box\/","url_meta":{"origin":530,"position":0},"title":"PCF8591 Analog To Digital - Braking out of the box","date":"July 16, 2016","format":false,"excerpt":"\u00a0 Yes you heard me right we are braking out of the box! We will be doing some soldering and making our own PCF8591 breakout board. In this example we are going to connect to PCF8591 AD\/DA Converter without using a pre-made breakout board. When connecting to PCF8591 AD\/DA Converter\u2026","rel":"","context":"Similar post","img":{"alt_text":"PCF8591Part2SC","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/07\/PCF8591Part2SC.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":674,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/i2c-logic-level-converter-building-our-own\/","url_meta":{"origin":530,"position":1},"title":"I2C logic level converter - Building our own","date":"May 29, 2016","format":false,"excerpt":"In this example we are going to create a I2C logic level converter. Disclaimer: We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI. The problem: Some I2C\u2026","rel":"","context":"Similar post","img":{"alt_text":"Be careful about which wires are actually connected and which are not. The SCL wires are crossing others but you see by the dots which they actually connect to.","src":"https:\/\/i0.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/I2CScheme.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":617,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/bmp180-barometer-sensor\/","url_meta":{"origin":530,"position":2},"title":"BMP180 Barometer sensor","date":"May 22, 2016","format":false,"excerpt":"In this example we are going to connect to BMP180 barometer sensor, we will be using brake-out board called GY-68. This sensor is really small even the full brake-outboard is only size of a fingernail. The board does not come with the header pins soldered on, but with mine the\u2026","rel":"","context":"Similar post","img":{"alt_text":"BMP180Soldered","src":"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2016\/05\/BMP180Soldered.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":224,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/working-with-leds-and-wiring-4-pin-rgb-led-to-raspberry-pi\/","url_meta":{"origin":530,"position":3},"title":"Working with LED's - And Wiring 4 pin RGB led to Raspberry PI","date":"October 12, 2015","format":false,"excerpt":"In this example we are going\u00a0learn some basics on how to calculate the resistor for LED, and we will take a look at how to wire up a 4 pin RGB LED. Disclaimer: We do not take any responsibility for possible errors in the guide or errors that you might\u2026","rel":"","context":"Similar post","img":{"alt_text":"RGBLedMutimeterTestNew","src":"https:\/\/i1.wp.com\/einhugur.com\/blog\/wp-content\/uploads\/2015\/10\/RGBLedMutimeterTestNew.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":94,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/","url_meta":{"origin":530,"position":4},"title":"Xojo GPIO","date":"September 19, 2015","format":false,"excerpt":"Guides for GPIO in Xojo \u00a0Infrared motion detection (Updated 1. May 2016) Shows\u00a02 ways of coding: Using Interrupts or simple polling. Sensors: \u00a0HC-SR501,\u00a0ST-00082 Mini,\u00a0Mini IR Pyroelectric Infrared PIR Motion Human Sensor Detector Module Uses:\u00a0Basic GPIO Relay board In this example we \u00a0wire up a relay board and let your Xojo\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":567,"url":"https:\/\/einhugur.net\/blog\/index.php\/xojo-gpio\/hd44780-based-lcd-display\/","url_meta":{"origin":530,"position":5},"title":"HD44780 based LCD display","date":"May 18, 2016","format":false,"excerpt":"In this example we are going to connect to HD44780 based LCD display. The display I am using \u00a0has 2 lines with 16 letters each. Same example will also work for HD44780 based displays that have 4 lines with 20 letters per line. Now before going\u00a0further then I want it\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/530"}],"collection":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":3,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/530\/revisions"}],"predecessor-version":[{"id":697,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/530\/revisions\/697"}],"up":[{"embeddable":true,"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/pages\/94"}],"wp:attachment":[{"href":"https:\/\/einhugur.net\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}