DS2482-800 1 Wire Setup using I2C

Have connected my ds18s20 directly to a pin and getting a constant reading of 85, 127.9375 …any suggestion?

using (var op = new OutputPort(Pin.P1_5, true))
            {
                var ow1 = new OneWire(op);
                var devs = OneWireBus.Scan(ow1, OneWireBus.Family.Unknown);

                foreach (var d in devs)
                    Debug.Print(d.Family.ToString());

                using (var s = new DS18B20(ow1, devs[0]))
                {
                    while (true)
                    {
                        // do the conversion and read the temperature all at once
                        Debug.Print(s.ConvertAndReadTemperature().ToString());

                        Thread.Sleep(1000);

                        // start the conversion (returns quickly)
                        s.StartConversion();

                        // wait for the conversion to happen
                        Thread.Sleep(1000);

                        // now, read the temp
                        Debug.Print(s.ReadTemperature().ToString());
                    }
                }

            }





using System;
using System.Collections;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace ox.fezcobra2
{
public static class OneWireBus
{
public class Device
{
public byte Address { get; private set; }

        public Family Family { get; private set; }

        internal Device(byte[] addr)
        {
            Address = addr;

            switch (addr[0])
            {
                case 0x10:
                    Family = OneWireBus.Family.DS18S20;
                    break;

                case 0x28:
                    Family = OneWireBus.Family.DS18B20;
                    break;

                default:
                    Family = OneWireBus.Family.Unknown;
                    break;
            }
        }
    }

    public enum Family : byte
    {
        Unknown = 0x00,
        DS18S20 = 0x10,
        DS18B20 = 0x28,
    }

    public static Device[] Scan(OneWire ow, params Family[] includeFamilies)
    {
        var list = new ArrayList();
        var all = false;
        var devs = ow.FindAllDevices();

        if (includeFamilies != null)
        {
            foreach (var f in includeFamilies)
            {
                if (f == Family.Unknown)
                    all = true;
            }
        }

        foreach (byte[] da in devs)
        {
            if (includeFamilies == null || includeFamilies.Length == 0 || all)
            {
                list.Add(new Device(da));
            }
            else
            {
                foreach (var f in includeFamilies)
                {
                    if (da[0] == (byte)f)
                        list.Add(new Device(da));
                }
            }
        }

        return (Device[])list.ToArray(typeof(Device));
    }

    public static Device[] Scan(Cpu.Pin pin, params Family[] includeFamilies)
    {
        using (var op = new OutputPort(pin, false))
            return Scan(new OneWire(op), includeFamilies);
    }
}

}




using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace ox.fezcobra2
{
    public class DS18B20 : IDisposable
    {
        private OutputPort m_op;
        private OneWire m_ow;
        private OneWireBus.Device m_dev;

        public DS18B20(Cpu.Pin pin) : this(pin, null) { }

        public DS18B20(Cpu.Pin pin, OneWireBus.Device dev)
        {
            m_op = new OutputPort(pin, false);
            m_ow = new OneWire(m_op);

            if (dev == null)
            {
                var devs = OneWireBus.Scan(m_ow, OneWireBus.Family.DS18B20);

                if (devs == null || devs.Length < 1)
                    throw new InvalidOperationException("No DS18B20 devices found on OneWire bus");

                dev = devs[0];
            }

            m_dev = dev;
        }

        public DS18B20(OneWire ow) : this(ow, null) { }

        public DS18B20(OneWire ow, OneWireBus.Device dev)
        {
            m_ow = ow;

            if (dev == null)
            {
                var devs = OneWireBus.Scan(ow, OneWireBus.Family.DS18B20);

                if (devs == null || devs.Length < 1)
                    throw new InvalidOperationException("No DS18B20 devices found on OneWire bus");

                dev = devs[0];
            }

            m_dev = dev;
        }

        public void Dispose()
        {
            if (m_op != null)
                m_op.Dispose();
        }

        public float ConvertAndReadTemperature()
        {
            var data = 0L;

            // if reset finds no devices, just return 0
            if (m_ow.TouchReset() == 0)
                return 0;

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // tell the device to start temp conversion
            m_ow.WriteByte(Command.StartTemperatureConversion);

            // wait for as long as it takes to do the temp conversion,
            // data sheet says ~750ms
            while (m_ow.ReadByte() == 0)
                System.Threading.Thread.Sleep(1);

            // reset the bus
            m_ow.TouchReset();

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // read the data from the sensor
            m_ow.WriteByte(Command.ReadScratchPad);

            // read the two bytes of data
            data = m_ow.ReadByte(); // LSB
            data |= (ushort)(m_ow.ReadByte() << 8); // MSB

            // reset the bus, we don't want more data than that
            m_ow.TouchReset();

            // returns C
            // F would be:  (float)((1.80 * (data / 16.00)) + 32.00);
            return (float)data / 16f;
        }

        public void StartConversion()
        {
            // if reset finds no devices, just return 0
            if (m_ow.TouchReset() == 0)
                return;

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // tell the device to start temp conversion
            m_ow.WriteByte(Command.StartTemperatureConversion);
        }

        public float ReadTemperature()
        {
            var data = 0L;

            // reset the bus
            m_ow.TouchReset();

            // address the device
            m_ow.WriteByte(Command.MatchROM);
            WriteBytes(m_dev.Address);

            // read the data from the sensor
            m_ow.WriteByte(Command.ReadScratchPad);

            // read the two bytes of data
            data = m_ow.ReadByte(); // LSB
            data |= (ushort)(m_ow.ReadByte() << 8); // MSB

            // reset the bus, we don't want more data than that
            m_ow.TouchReset();

            // returns C
            // F would be:  (float)((1.80 * (data / 16.00)) + 32.00);
            return (float)data / 16f;
        }

        public static float ToFahrenheit(float tempC)
        {
            return (9f / 5f) * tempC + 32f;
        }

        private void WriteBytes(byte[] data)
        {
            for (var i = 0; i < data.Length; i++)
                m_ow.WriteByte(data[i]);
        }

        private static class Command
        {
            public const byte SearchROM = 0xF0;
            public const byte ReadROM = 0x33;
            public const byte MatchROM = 0x55;
            public const byte SkipROM = 0xCC;
            public const byte AlarmSearch = 0xEC;
            public const byte StartTemperatureConversion = 0x44;
            public const byte ReadScratchPad = 0xBE;
            public const byte WriteScratchPad = 0x4E;
            public const byte CopySratchPad = 0x48;
            public const byte RecallEEPROM = 0xB8;
            public const byte ReadPowerSupply = 0xB4;
        }
    }
}

Two. First, fix up your code tags above.

Second, “85” is a common reading when you have not started a conversion correctly, or where power is not wired correctly. How have you got the device connected? Are you using the “P” designated device or are you trying to use parasitic mode, and if so what resistor value are you using?

Using 4.7k resistor from data(Pin2,Middle) to 5v. Pins 1 and 3 connected to ground. Using 18B20 device

thanks

Still didn’t edit the above and fix the code tag though :wink:

Your problem now is that the conversion isn’t running. With parasitic power like this, you need to behave explicitly by holding the line high - take a look at Figure 4 in the datasheet, you can see they use a separate pin to do that. You either need to do that or move away from Parasitic power mode (in my view it’s simpler to wire as per figure 5, Vdd / pin3 connects to 5v)

1 Like

@ Brett your awesome…it works thanks to you :slight_smile: anytime i can return the favor i will…my project is progressing well…next step…start using the DS2482-800