{"id":1134,"date":"2017-07-14T15:49:16","date_gmt":"2017-07-14T14:49:16","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=1134"},"modified":"2017-07-26T14:38:10","modified_gmt":"2017-07-26T13:38:10","slug":"codatex-rfid-sensor","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2017\/codatex-rfid-sensor","title":{"rendered":"Codatex RFID Sensor"},"content":{"rendered":"<p>Some time ago, when I started to use RFID tags with an <a href=\"http:\/\/ofalcao.pt\/blog\/2015\/modulo-mfl-em-tomar\">automated LEGO train<\/a>, I found out that there was a RFID sensor available for the MINDSTORMS NXT, the <a href=\"http:\/\/www.codatex.com\/lego-sensor.html\">Codatex RFID Sensor for NXT<\/a>:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/www.codatex.com\/images\/LEGO-RFID-Sensor-Verpackung.jpg?w=840\" \/><\/p>\n<p>Codatex doesn&#8217;t make them aymore so I ordered one from BrickLink but never got it working with ev3dev. I put it on the shelf hoping that after a while, with ev3dev constant evolution, things would get better.<\/p>\n<p>Last month Michael Brandl told me that the Codatex sensors were in fact LEGO sensors and he asked if it was possible to use with EV3.<\/p>\n<p>Well, it is possible, just not with original LEGO firmware. At least <a href=\"https:\/\/github.com\/SnakeSVx\/ev3\/blob\/master\/Lejos\/src\/main\/java\/lejos\/hardware\/sensor\/RFIDSensor.java\">LeJOS<\/a> and <a href=\"http:\/\/botbench.com\/driversuite\/codatech-rfid_8h_source.html\">RobotC<\/a> have support for the Codatex RFID Sensor. But not ev3dev \ud83d\ude41<\/p>\n<p>So this holidays I put the Codatex sensor on my case, decided to give it another try.<\/p>\n<p>I read the documentation from Codatex and the source code from LeJOS and RobotC and after a while I was reading the sensor properties directly from shell.<\/p>\n<p>After connecting the sensor to Input Port 1 I need to set the port mode to &#8220;other-i2C&#8221;:<\/p>\n<pre>echo other-i2c &gt; \/sys\/class\/lego-port\/port0\/mode<\/pre>\n<p>Currently there are two I2C modes in ev3dev: &#8220;nxt-i2c&#8221; for known NXT devices and &#8220;other-i2c&#8221; for other I2C devices, preventing the system to poll the I2C bus.<\/p>\n<p>To read all the Codatex registers this line is enough:<\/p>\n<pre>\/usr\/sbin\/i2cset -y 3 0x02 0x00 ; \/usr\/sbin\/i2cset -y 3 0x02 0x41 0x83; sleep 0.1; \/usr\/sbin\/i2cdump -y 3 0x02<\/pre>\n<p>(first wake up the sensor with a dummy write, then initialize the firmware, wait a bit and read everything)<\/p>\n<pre>Error: Write failed\r\nNo size specified (using byte-data access)\r\n 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef\r\n00: 56 31 2e 30 00 00 00 00 43 4f 44 41 54 45 58 00 V1.0....CODATEX.\r\n10: 52 46 49 44 00 00 00 00 00 00 00 00 00 00 00 00 RFID............\r\n20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\n90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\na0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\nb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\nc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\nd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\ne0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\r\nf0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................<\/pre>\n<p>The &#8220;Error: Write failed&#8221; is expected because the dummy write that wakes the Codatex sensor fails.<\/p>\n<p>I got excited, it seemed easy.<\/p>\n<p>So to read just once (singleshot mode) this should work:<\/p>\n<pre>\/usr\/sbin\/i2cset -y 3 0x02 0x00 ; \/usr\/sbin\/i2cset -y 3 0x02 0x41 0x01; sleep 0.25; \/usr\/sbin\/i2cdump -r 0x42-0x46 -y 3 0x0<\/pre>\n<p>(wake up, send a singleshot read command, wait for aquiring, read the 5 Tag ID registers)<\/p>\n<p>But it didn&#8217;t work:<\/p>\n<pre>Error: Write failed\r\nNo size specified (using byte-data access)\r\n 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef\r\n40: 00 00 00 00 00 .....<\/pre>\n<p>The next days I read lots of code from the web, even from\u00a0<a href=\"http:\/\/www.codatex.com\/picture\/upload\/en\/RFID_NXC_lib.zip\">Daniele Benedettelli old NXC library<\/a>. It seemed that my timings were wrong but I could not understand why.<\/p>\n<p>After <a href=\"https:\/\/github.com\/ev3dev\/ev3dev\/issues\/913\">asking for help at ev3dev<\/a> I found the reason: the Codatex sensor needs power at pin 1 for the RFID part. So the I2C works but without 9V at pin 1 all readings will fail.<\/p>\n<p>LeJOS and RobotC activate power on pin1 but the two ev3dev I2C modes don&#8217;t do that. ITo be sure I tried LeJOS and finally I could read my tags:<\/p>\n<pre><code>package PackageCodatex;\r\n\r\nimport lejos.hardware.Brick;\r\nimport lejos.hardware.port.SensorPort;\r\nimport lejos.hardware.sensor.RFIDSensor;\r\n\r\npublic class Codatex\r\n{\r\n    public static void main(String[] args)\r\n    {\r\n    \tRFIDSensor rfid = new RFIDSensor(SensorPort.S1);\r\n\t\tSystem.out.println(rfid.getProductID());\r\n\t\tSystem.out.println(rfid.getVendorID());\r\n\t\tSystem.out.println(rfid.getVersion());\r\n\t\ttry {Thread.sleep(2000);}\r\n\t\tcatch (InterruptedException e) {}\r\n\t\tlong transID = rfid.readTransponderAsLong(true);    \r\n\t\tSystem.out.println(transID);\r\n\t\ttry {Thread.sleep(5000);}\r\n\t\tcatch (InterruptedException e) {}\r\n\t}\r\n}<\/code><\/pre>\n<p>David Lechner gave some hint on how to write a driver but honestly I don&#8217;t understand at least half of it. So I made a ghetto adapter with a MINDSTORMS EV3 cable and a 9V PP3 battery and it finally works &#8211; I can read the Codatex 4102 tags as well as my other 4001 tags:<\/p>\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"Codatex RFID Sensor with EV3 and ev3dev\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/ONw08QqMvhc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<p>I created a <a href=\"https:\/\/github.com\/JorgePe\/CodatexRFID-ev3dev\">GitHub repository<\/a> with the scrips to initialize the port and to use singleshot reads. Soon I will add a script for countinuous reads.<\/p>\n<p>An important note for those who might try the ghetto cable: despite many pictures on the web saying that pin 2 is Ground, IT IS NOT. Use pin 1 for power and pin 3 for Ground. And preferably cut the power wire so that if you make some mistake your EV3 pin1 is safe.<\/p>\n<p>Now I really do need to learn how to write a driver instead of hacking cables. Ouch!<\/p>\n<p>Update: Since <a href=\"https:\/\/github.com\/ev3dev\/ev3dev\/issues\/913\">yesterday<\/a> (25-July-2017) the ghetto cable is no longer needed. David Lechner added the pin1 activation feature to the &#8220;other-i2c&#8221; mode so since kernel <strong>4.4.78-21-ev3dev-ev3<\/strong> a standard cable is enough.<\/p>\n<p>Thank you David!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, when I started to use RFID tags with an automated LEGO train, I found out that there was a RFID sensor available for the MINDSTORMS NXT, the Codatex RFID Sensor for NXT: Codatex doesn&#8217;t make them aymore so I ordered one from BrickLink but never got it working with ev3dev. I put &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2017\/codatex-rfid-sensor\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;Codatex RFID Sensor&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[13,157,154,18,158,19,148,20],"tags":[309,308],"series":[],"class_list":["post-1134","post","type-post","status-publish","format-standard","hentry","category-ev3dev","category-ev3dev-en","category-lego-en","category-lego","category-lego-mindstorms-en","category-lego-mindstorms","category-linux-en","category-linux","tag-codatex","tag-rfid"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-ii","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1134"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1134\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1134"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}