Wednesday, December 19, 2012

I2C Analog to Digital Converter

The first device I hooked to my Raspberry Pi is based on the PCF8591 Analog to Digital Converter (ADC). This chip has 4 analog inputs (ADC) and one analog output or Digital to Analog Converter (DAC).

I am using a pre-assembled board from Deal Extreme which comes with the chip, a temperature sensor, light sensor, variable resistor and LED. This provides a simple showcase for the chip and more importantly, it has a light sensor which is important to my project. The board was only a few dollars http://dx.com/p/pcf8591-8-bit-a-d-d-a-converter-module-150190 there are also other similar boards on there.

PCF8591 demo board. GPIO pins are visible on the right.


The first step is to physically hook up the board. Mine came with the required cables (often called dupont cables) which is also a handy way to start. The cables must be connected to the Raspberry Pi GPIO pins nominated for I2C. These have the required 'pull up resistors' already installed. (These are what make the wires operate like a bus). The pins are
  • P1-01 +3.3v (VCC)
  • P1-03 Data (SDA)
  • P1-05 Clock (SCL)
  • P1-09 Ground (GND)
Raspberry Pi showing GPIO cables connected.


My demo board has a red power indicator LED which came on once I powered up.

The next big test is to see if the i2c driver can talk to your chip. The Raspberry Pi actually comes configured with two I2C buses and for reasons unknown, on my system the bus labelled I2C0 is allocated the Linux device i2c-1.

Scanning both buses won't hurt.

jnewbigin@raspberrypi:~$ i2cdetect 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --                         


You can see here that a device has been detected at address 0x48. This is the expected address for my chip so that means we are in business.

Reading and writing to the chip is quite straight forward but the chip does have a few nuances.  The first read after power on will return 0x80. The analog to digital conversion is performed when you make a read request but the read will return the previous sample so it is always one sample behind. This is not too confusing unless you are switching to read a different input.

I will show how to read the data using the command line tools i2cget and i2cset. In another blog post I will show how you can interface with the chip from c code.

All these commands take a parameter 1 to specify which i2c bus and I pass -y which skips the safety warning. (We know what we are doing so this is OK. On other hardware such as your PC, things can and do go wrong by using these commands).

The most basic read is the default channel (input 0).
jnewbigin@raspberrypi:~$ i2cget -y 1 0x48
0x80
That is the power on status code. Now we read again

jnewbigin@raspberrypi:~$ i2cget -y 1 0x48
0xd2
That is the value that was sampled when we made our first read (the one that returned 0x80).
Now cover up the light sensor and read again

jnewbigin@raspberrypi:~$ i2cget -y 1 0x48
0xd2
Yep, no change. The new value has been sampled so now we read it
jnewbigin@raspberrypi:~$ i2cget -y 1 0x48
0xeb
Now we get the new value.

Now, switch to read another input, input number 1
jnewbigin@raspberrypi:~$ i2cset -y 1 0x48 0x01
jnewbigin@raspberrypi:~$ i2cget -y 1 0x48
0xeb
First we get an old value.
jnewbigin@raspberrypi:~$ i2cget -y 1 0x48
0xcf
Then the new value

We can repeat to select channel 2 and 3.

We can enable the analog output by adding bit 0x40 to the set command and then specify a value for the DAC
jnewbigin@raspberrypi:~$ i2cset -y 1 0x48 0x41 0xff

And the indicator LED turns on

jnewbigin@raspberrypi:~$ i2cset -y 1 0x48 0x41 0x00
And the indicator LED turns off. You can of course set it to any value between 0x00 and 0xff and see the LED dim and turn off. (You can also see why LEDs don't make good analog indicators).


66 comments:

  1. I have the same board as you and trying to find out how to deal with the jumpers, LDR, thermistor and potentiometer.

    Did you figure it out?

    ReplyDelete
    Replies
    1. I found needed information at http://brainfyre.wordpress.com/2012/10/25/pcf8591-yl-40-ad-da-module-review/

      Delete
  2. "We can enable the analog output by adding bit 0x40 to the set command and then specify a value for the ADC"
    ...I think you mean setting the value for the DAC.
    Mr T.

    ReplyDelete
  3. Hi there

    I need to operate 4 DAC channels in parallel.
    Can this be done with one RasPi and 4 of the DX modules?

    Thanx -Didi

    ReplyDelete
    Replies
    1. Sure. The PCF8591 has a 3 bit address which allows you to use up to 8 of these on one bus.
      I am not sure what is required to change the address on the DX demo board. You may have to cut some tracks on the PCB. Check out the data sheet to see which pins are used for addressing.
      There may be other boards which have jumpers for addressing which might be easier.

      Delete
  4. To anyone else who has used these YL-40 modules: have you managed to get any "useful" (I know, it's only 8-bit, but still) data from the integrated thermistor?

    On mine, everything else seems to work just fine (on-board sensors as well as all using external, at least w/a trimpot wired to each, and the output seems appropriate), but the built-in thermistor doesn't seem to respond, much.

    For a ~25°F (~15°C) temperature drop, it doesn't give more than a few "bits" increase on the data channel to the RaspPi (~211 up to ~216). I've also measured the voltage through AIN1 (seems appropriate relative to Vcc), as well as the resistance while powered off (not entirely sure this measurement is "valid"): the YL-40's thermistor (same conditions as previously mentioned) only increases by about 350ohms; for comparison, an old Radio-Shack thermistor (10Kohm, also "bead" type, unfortunately no longer sold, but you could look it up #271-0110) jumps almost 15Kohms !

    So, I'm thinking I've either got a YL-40 with a "bad" thermistor, or the thermistor is just completely "not suitable" for this "application" ...

    Again, is ANYONE getting "reasonable" data from the thermistor on a YL-40 ?

    If so, am I missing something? As I mentioned, I can get "data" (such as it is, but "responsive") for everything else except the thermistor (using either the I2CTools used in John's post here, or my own Python scripts

    Thanks in advance for any help!

    Mike

    ReplyDelete
  5. If your still having trouble getting valid thermistor reading, checkout my C based helpers at https://github.com/skoona/skn_rpi-display-services. There is a client in that package that reads all the inputs with valid results.

    ReplyDelete
  6. I have the same module but the Raspberry Pi, but then I execute:
    i2cget -y 1 0x48
    or i2cget -y 1 0x48 0x01 or i2cget -y 1 0x48 0x02 or i2cget -y 1 0x48 0x03 i am always receve:
    0x00
    Why?

    ReplyDelete
  7. The datasheet for the chip provides a good reference: http://www.nxp.com/documents/data_sheet/PCF8591.pdf

    ReplyDelete
  8. This article is very interesting. Sara

    ReplyDelete
  9. I found a lot of information here to create this actually best for all newbie here tweak box apkxyz. Thank you for this information.

    ReplyDelete
  10. Rapid Essay https://rapidessay.com/research-paper-writing-service.html is a research paper writing service that works on every single order from scratch for you to submit original essays and avoid accusations of plagiarism. Our writing company follows your guidelines and deadlines. Every completed order meets your expectations and is relevant to the topic assigned to you.

    ReplyDelete
  11. Termpapersite is the best research papers company https://termpapersite.com/research-paper-writing-service/ they follow rules and guidelines based on client requirements,

    ReplyDelete
  12. Want to know how to write a scholarship essay? Writing a scholarship essay may cause stress and frustration for an individual. No wonder, they need to study the requirements set by an educational institution in order to meet them precisely.

    ReplyDelete
  13. Edumagnate https://www.edumagnate.com/ is a online paper help company where you can ask write to write an dissertation or research paper based on analog and digital converter. They have team of paper helper to serve on time.

    ReplyDelete
  14. Digital security is one of the most critical things in the modern world. Hackers and spammers are continually keeping a vicious eye on your sensitive data that can be used to threaten the safety and security of your personal and professional life. To avoid any unauthorized access to your system and unwanted use of your data, you should make every possible effort to keep your system secured, and McAfee Antivirus is the best solution for that. You can get this reliable antivirus by visiting Mcafee.com/activate, and it will start protecting your device against viruses, malware, phishing, and every possible digital element that can threaten the safety and functioning of your system. McAfee Antivirus is compatible with all the leading operating systems, including Windows, macOS, Linux, and Android. For more information, please visit Mcafee.com/activate.
    Mcafee.com/activate | Mcafee.com/activate | Mcafee.com/activate

    ReplyDelete
  15. thanks for sharing
    Check your price with HIGS!
    Most professionalized and passionate research paper writers at an affordable price! Grab it now for the best offers & discounts! Check your price quotes by clicking

    Research paper writing service

    ReplyDelete
  16. Hi, QuickBooks is one of the best and essential accounting software in the business world, but it has some common errors which needs a solution for its proper working,one of the common error is quickbooks error 1334 repair,to know about this error in detail you can visit the given site.

    ReplyDelete
  17. That is a informative post! Thanks for posting.
    Converting quicken to quickbooks was never so easy as it is now with all new Quickbooks tool. Just by using a tool you can upgrade to new software within few hours if you have a large business. Don't loose your files, convert now.

    ReplyDelete
  18. Thanks for sharing such informative blog. A DNP capstone project is quite a long academic document. It takes a lot of time to write it and as such, students are exhausted by the time that they finish writing it. By requesting for our DNP Editing Services, you can boost your chance of submitting a quality paper.

    ReplyDelete
  19. To rock the world, you first have to finish your homework, and we as a source of computer science assignment help are here to help you with that: programminggeeks

    ReplyDelete
  20. Nice Blog !
    All of us have been affected by this pandemic in some way or another. In such challenging hours, we at QuickBooks Customer Service assure you that you will get the best service for QuickBooks.

    ReplyDelete
  21. This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog..
    Moviesda

    ReplyDelete
  22. if you're looking this "error 15241 quickbooks" type of error fixing method than click below

    ReplyDelete
  23. com error quickbooks 2017 This error can happen anytime but mostly if you are accessing the QuickBooks software or exiting it then faced it

    ReplyDelete
  24. Thanks for sharing such informative article. At our writing company, we understand the ordeal of DNP nursing scholars. This is why we have professional DNP coursework writing help at their disposal.

    ReplyDelete
  25. quickbooks database server manager (QBDBMS) is an astonishing utility part by Intuit. These mechanical assembly abilities to assemble the QuickBooks laborer envelope for multi-customer mode. It helps in profiting by multi-customer mode.

    ReplyDelete
  26. This mistake can take place whenever but mostly in case you are having access to the quickbooks software program or exiting it then faced it thank you for can someone write my assignment UK for me quick sharing such an informative weblog. A dnp capstone task is pretty an extended academic report.

    ReplyDelete
  27. Thanks for the detailed instructions, I am an author and it is very difficult for me to cope with technical problems.
    Yaro, eduloh

    ReplyDelete
  28. The 96 well plate with square holes and flat transparent bottom is used for all large-scale laboratory applications, and is very suitable for cell and tissue culture, sample storage, etc.

    ReplyDelete
  29. Visit
    www.hp.com/go/wirelessprinting and open the door to the world of HP smart printing solutions. HP wireless printer is a versatile printing device that helps you print, scan, copy and fax your documents as per the requirement.
    Visit ij.start.canon | ij.start canon and find out the best way to download Canon printer drivers. Canon printers are ideal for every situation wherever you need a document, paper, or photo print or even if you wish to scan, fax, and do more ijstart.canon will make you learn how to set up a canon printer to get advanced printing features.

    ReplyDelete
  30. Canon IJ Network Tool is a toolkit software with the options to keep a check on most of your Canon printer network settings and adjust them according to your requirements. The Canon IJ Network tool will get you through the network settings uninterruptedly. Canon IJ Printer Utility is a complete software package meant to adjust and modify the configurations of your printer device as per the requirement.

    ReplyDelete
  31. هذه الشركة توفر خدمات تسليك و تنظيف مواسير الصرف الصحي ، و كذلك توفر أيضا خدمات تنظيف البيارات و غرف التفتيش ، مع الاعتماد على مواد حديثة تعمل على إزابة التكتلات الموجودة بالصرف و التي أدت إلى الانسداد ، و كذلك تقنيات و أجهزة توفر الكشف عن مكان التعطل ، هذا مع توفير إمكانية شفط القاذورات المتراكمة في المكان الذي أصيب بالانسداد ، و تفير خدمات الكشف عن اختلاط الصرف بالمياه
    شركة تسليك مجاري بالرياض
    المسبح من أهم الأشياء التي تستخدم ربما كشكل ديكوري بداخل المنزل ، أو أنها تستخدم لمزيد من المتعة داخل المنزل ، و لابد من الاهتمام بتنظيفها بشكل دوري حتى لا تكون سببا في نقل الميكروبات و الأمراض ، و هناك العديد من الشركات التي تعمل على تنظيف المسابح بالرياض بأعلى مستوى و منها على سبيل شركة أرض الانجاز
    شركة تنظيف مسابح بالرياض
    الخزانات من أهم الأشياء التي لابد من وجودها في المنزل ، و ذلك لأنها تعمل على حفظ المياه باستمرار في وقت انقطاعها ، و كذلك تعمل على حفظها بجودة عالية ، و هناك العديد من الشركات التي توفر خدمات عزل الخزانات بالرياض و منها على سبيل المثال شركة أرض الانجاز
    شركة عزل خزانات بالرياض

    ReplyDelete
  32. Read Newspaper Onlinelist sorted by Countries and by languages. A huge collection of newspaper or epapers online from all over the world in various languages. Epaper hub contains Print edition epaper that you read Online and grab your news through internet. Here you can find any number of epaper in different language, from different cities, different countries and cities. So don’t wait and search you daily newspaper and enjoy news from all over the world online.

    ReplyDelete
  33. Thanks for sharing this great blog. check here

    ReplyDelete
  34. Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some original thoughts on this subject. realy thank you for starting this up. this website is something that is needed on the web, someone with a little originality. useful job for bringing something new to the internet!

    Metal-archives.com
    Information
    Click Here
    Visit Web

    ReplyDelete
  35. Great work. Thanks for sharing.
    https://www.excavationla.com/
    https://www.concretesanluisobispo.com/

    ReplyDelete
  36. We will do your essay as we offer quality online paper help, and we are the best if you’re searching to take online paper help or philosophy paper writing

    ReplyDelete
  37. Canon IJ Network Tool will get you through the network settings uninterruptedly. It is essentially required when you are attempting to get your printer connected to a different network because a new network tends to reset the printer’s existing network settings.The Canon IJ Printer Utility can be used to keep a check on your printer’s ink levels and cartridges and clean the ink tanks and paper feed rollers. Also, you can make adjustments to your Canon printer’s power settings.

    ReplyDelete
  38. Make sure the surface where you are placing the printer is clean and close to Pc or laptop.Check the shipped components with your inkjet printer
    http //ij.start.canon,
    https //ij.start.cannon

    ReplyDelete
  39. Allows you to download, install, and configure setup on your PC, Mac and smartphones as well. Canon ij setup is the software to install Canon printer wirelessly.
    ij.start.canon,
    ij.start canon

    ReplyDelete
  40. ij.start.canon
    is the manufacturer site to download Canon printer drivers. Install and set up Canon Printer from https: //ij.start.canon and obtain high-quality printing documents straightforwardly.

    https//ij.start.cannon is actually the official site of ij start canon that helps you install the latest printer drivers and software. Visiting http //ij.start.cannon
    provides you a complete list of all canon printers where you’ve to select your printer model and download the correct setup file

    ReplyDelete
  41. Canon IJ Network Tool is a toolkit software with the options to keep a check on most of your Canon printer network settings and adjust them according to your requirements.

    Canon IJ Printer Utility is a complete software package meant to adjust and modify the configurations of your printer device as per the requirement. It is one of the essential utility software offered by Canon to ease your printer configuration alteration using a few clicks.


    Canon.com/ijsetup/mg2500
    is the Canon support link to get you the latest printer drivers and software to set up your PIXMA MG2500 or MG2520.

    ReplyDelete
  42. Once you are done with the driver setup via canon.com/ijsetup , you will have to insert a pile of pages into the printer tray for printing the documents..

    Visit ij.start.canon | ij.start canon and find out the best way to download Canon printer drivers.

    All-in-one Canon Inkjet printers are suitable for home, business, school, and others to improve productivity. You can easily set up your Canon printer through drivers from Canon.com/ijsetup , wireless connection, USB, and a few components.

    ReplyDelete
  43. The setup process for every Canon model is almost similar, however the download through https //ij.start.cannon or http //ij.start.cannon and installation process may differ.Depending on your requirement, it offers a type printer including PIXMA, SELPHY, MAXIFY, etc. canon.com/ijsetup Some factors need to be in mind while choosing an inkjet printer for you. Later, you can easily set up your Canon printer through drivers from ij.start.cannon wireless connection, USB, and a few components. Https //ij.start.cannon

    ReplyDelete
  44. We offer the required data to configure, utilize and install your Canon products on your Windows PC.canon is completely safe and secure. ij.start.canon |
    ij.start.cannon

    ReplyDelete
  45. Nice posting, I will share it with my contacts. Thank you very much for this post.

    ij.start.canon
    ij.start.canon

    ReplyDelete

  46. Amazing website, Love it. Great work done. Nice website. Love it. This is really nice.

    FMovies | FMovies


    ReplyDelete
  47. Amazing article, thank you! Enjoy my site yarkoweb.casino

    ReplyDelete
  48. Nice...
    It's a very powerful article. I really like this post. Thank you so much for sharing this.

    putlocker
    kissanime

    ReplyDelete
  49. This is very nice and this article is very helpful for everyone. I like it very much please keep doing this amazing work. Eid ul Fitr 2022 SMS

    ReplyDelete
  50. HI, THIS IS WAS A VERY GOOD POST INDEED. WE WILL SURELY COME AND VISIT THIS BLOG MORE OFTEN.
    온라인섯다

    ReplyDelete
  51. GOOD DAY! I AM VERY THANKFUL FOR THIS, THANK YOU FOR SHARING THIS.
    HAVE A GREAT DAY!
    성인웹툰

    ReplyDelete
  52. Get an official support of ij.start.canon and ij.start.cannon on our website. Here you will get a secure and easy platform to download canon setup.

    ReplyDelete
  53. Welcome to the most attractive and prestigious city in the world. Dubai offers you the best routes for a tour. If you are planning a visit or just want to explore new places, then we have listed some of the best tour packages from Delhi such as dubai packages from delhi.
    Tourists can explore the rich cultures and heritage, great attractions, and amazing shopping facilities offered by DiplomatVisa.Com. From 5-star hotels to delicious food, Dubai has everything at your convenience.
    Please take a look at other Premium Packages Offered By DiplomatVisa.Com:
    1. bangkok tour packages
    2. tour packages for canada
    3. bali packages from delhi
    4. usa tour packages
    For exciting offers and more information,
    Visit our official website: https://diplomatvisa.com/dubai-tour-packages-from-delhi/

    ReplyDelete