I have been looking through the forum to find a solution, but with no success... This is my issue, for some reason I get this strange data stream output when communicating with a Devantech SRF02 over I2C:
Waving my hand in front of it:Distance in inches : 25856Distance in centimeters : 512Distance in microseconds : 43267Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 36355Distance in inches : 21760Distance in centimeters : 5376Distance in microseconds : 18947Distance in inches : 1792Distance in centimeters : 7424Distance in microseconds : 9731Distance in inches : 25856Distance in centimeters : 5376Distance in microseconds : 23811Distance in inches : 3072Distance in centimeters : 7936Distance in microseconds : 37379Distance in inches : 25856Distance in centimeters : 7936Distance in microseconds : 38403Distance in inches : 2048Distance in centimeters : 39936Distance in microseconds : 13059Distance in inches : 22528Distance in centimeters : 1024Distance in microseconds : 28931Distance in inches : 25856Distance in centimeters : 39680Distance in microseconds : 34819Distance in inches : 26112Distance in centimeters : 4352Distance in microseconds : 17667Distance in inches : 2048Distance in centimeters : 56832Distance in microseconds : 60931Distance in inches : 25600Distance in centimeters : 39424Distance in microseconds : 15107Distance in inches : 25856Distance in centimeters : 46848Distance in microseconds : 34819Distance in inches : 3072Distance in centimeters : 39936Distance in microseconds : 41475Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 35587Distance in inches : 25856Distance in centimeters : 7936Distance in microseconds : 10499Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 20227Not waving my hand in front of it:Distance in inches : 15616Distance in centimeters : 768Distance in microseconds : 44547Distance in inches : 15616Distance in centimeters : 512Distance in microseconds : 32259Distance in inches : 25856Distance in centimeters : 512Distance in microseconds : 38915Distance in inches : 25856Distance in centimeters : 512Distance in microseconds : 51971Distance in inches : 25856Distance in centimeters : 39680Distance in microseconds : 38147Distance in inches : 25856Distance in centimeters : 768Distance in microseconds : 41475Distance in inches : 25856Distance in centimeters : 512Distance in microseconds : 32259Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 46339Distance in inches : 25856Distance in centimeters : 40192Distance in microseconds : 38147Distance in inches : 15616Distance in centimeters : 512Distance in microseconds : 41987Distance in inches : 25856Distance in centimeters : 40192Distance in microseconds : 50947Distance in inches : 15616Distance in centimeters : 512Distance in microseconds : 56067Distance in inches : 15616Distance in centimeters : 40192Distance in microseconds : 43779Distance in inches : 15872Distance in centimeters : 512Distance in microseconds : 24067Distance in inches : 15616Distance in centimeters : 40192Distance in microseconds : 36099Distance in inches : 15616Distance in centimeters : 40192Distance in microseconds : 41475Distance in inches : 25856Distance in centimeters : 768Distance in microseconds : 46595Distance in inches : 25856Distance in centimeters : 1024Distance in microseconds : 43779Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 33027Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 27139Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 33027Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 20739Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 33027Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 33027Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 27139Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 27139Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 20227Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 30979Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 24067Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 25347Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 43267Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 18179Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 18179Distance in inches : 15616Distance in centimeters : 39936Distance in microseconds : 24579Distance in inches : 15616Distance in centimeters : 39680Distance in microseconds : 30467
I have it connected to my Netduino Plus through a breadboard, here is the connection scheme: UltrasonicRangeFinderSensor_SRF02_I2C_bb.png 248.01KB 0 downloads And some links for the components: Devantech SRF02 I2C documentation, Adafruit Logic Level Converter with 10k pull-ups. And as well the code I'm using, SDK 4.2 is used. BTW is the value of the Execute methods timeout really needed to be 1000 ms when I have the Thread.Sleep method at 80 ms before reading from the register of the device? I have as well set the I2C clock to 100kHz, but still getting the same readings with 400kHz.
using System;using System.IO.Ports;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;namespace UltrasonicRangeFinderSensor{ public class I2C_SRF02 { private static InterruptPort button = new InterruptPort( Pins.ONBOARD_SW1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLevelLow); private static I2CDevice.Configuration con = new I2CDevice.Configuration(0x70, 400); // Most significant bits, 7-bit private static I2CDevice srf02 = new I2CDevice(con); // First transaction is writing the "read command" // Second transaction is reading the data private static I2CDevice.I2CTransaction[] xActions; // create read buffer to read the register private static byte[] RegisterValue = new byte[2]; private static System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); private static byte[] queryResult = new byte[2]; enum RealResultType : byte { Inch = 0x50, Centimeters = 0x51, Microseconds = 0x52 }; public static void Main() { using (srf02) { button.OnInterrupt += button_OnInterrupt; Thread.Sleep(Timeout.Infinite); } } private static void button_OnInterrupt(uint port, uint state, DateTime time) { getRangeResults(); button.ClearInterrupt(); } private static void getRangeCommand(RealResultType resultType) { xActions = new I2CDevice.I2CTransaction[1]; // Issuing a ranging xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00, (byte)resultType }); // if the execute command returns zero, the transaction failed (this // is a good check to make sure that you are communicating with the device correctly // and don’t have a wiring issue or other problem with the I2C device) if (srf02.Execute(xActions, 1000) == 0) { Debug.Print("Failed to perform I2C transaction"); } Thread.Sleep(80); // Letting the N+ sleep until the ranging is done xActions = new I2CDevice.I2CTransaction[4]; // Setting the register pointer to echo #1 register to retrieve the result // and Requesting the result of the ranging xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x02 }); xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); xActions[2] = I2CDevice.CreateWriteTransaction(new byte[] { 0x03 }); xActions[3] = I2CDevice.CreateReadTransaction(RegisterValue); if (srf02.Execute(xActions, 1000) == 0) { Debug.Print("Failed to perform I2C transaction"); } else { if (resultType.Equals(RealResultType.Centimeters)) { Debug.Print("Distance in centimeters : " + (ushort)((RegisterValue[0] << 8) | RegisterValue[1])); } else if (resultType.Equals(RealResultType.Inch)) { Debug.Print("Distance in inches : " + (ushort)((RegisterValue[0] << 8) | RegisterValue[1])); } else if (resultType.Equals(RealResultType.Microseconds)) { Debug.Print("Distance in microseconds : " + (ushort)((RegisterValue[0] << 8) | RegisterValue[1])); } } } private static void getRangeResults() { getRangeCommand(RealResultType.Inch); Thread.Sleep(100); getRangeCommand(RealResultType.Centimeters); Thread.Sleep(100); getRangeCommand(RealResultType.Microseconds); Thread.Sleep(100); } }}
Could it be that I have damaged my board in some way? Because before without knowing that the pins are 5V tolerant but the logic is only 3.3V tolerant I only used pull-ups and not the logic level converter, so I had 5V directly to the analog pins... Or has something happened to my SRF02? Static electricity? Rather a newbie at I2C communication, most of the code is based on examples I have found here and on a variety of wikis. Regards, TM