Read ds2401 with 1-wire protocol

Hi Sir,
I 'm new in this forum.

I am trying to read ds2401 with 1-wire protocol.
Are there any examples where to take the lead?

Thank you
Alex

If you are trying to read the temperature I am using the TMP36 here:

This is the class library I wrote is here.

Imports System
Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
'Need reference to SecretLabs.NETMF.Hardware.AnalogInput.dll
Imports SecretLabs.NETMF.Hardware.AnalogInput
Imports SecretLabs.NETMF.Hardware.Netduino
Imports Microsoft.VisualBasic.Strings

‘’’


‘’’ Class to read the Tmp36 temperature sensor
‘’’ Avaliable from AdaFruit.com
‘’’ Set the TemperatureAdjustment if needed
‘’’ Open PortAnalogPort
‘’’ GetTemperature will return a string in Fahrenheit and the degree char
‘’’

‘’’
Public Class Tmp36

Private Shared Port As SecretLabs.NETMF.Hardware.AnalogInput
'Changed 12/15/15
'Public Shared TemperatureAdjustment As Integer = 0
Public Shared TemperatureAdjustment As Integer = -2
Public Shared CurrentTemperature As String = String.Empty

Public Shared NewThread As Thread = New Thread(AddressOf PollTemperature)

Public Shared Sub PollTemperature()
    While True
        GetTemperature()

        '5 min.
        Thread.Sleep(300000)
    End While
End Sub

Public Shared Sub OpenAnalogPort(Optional AnalogInputPin As Integer = 0)

    Try

        Select Case AnalogInputPin
            Case 0
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0)
            Case 1
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A1)

            Case 2
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A2)

            Case 3
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A3)

            Case 4
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A4)

            Case 5
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A5)
            Case Else
                Port = New SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0)
        End Select

    Catch ex As Exception

        Debug.Print("Error in OpenAnalogPort " & AnalogInputPin.ToString & ": " & ex.ToString)
    End Try

End Sub

Public Shared Sub GetTemperature()

    Try

        Dim CurrentValue As Double = 0
        Dim CurrentVoltage As Single = 0
        Dim Temperature As Single = 0
        Dim Fahrenheit As Integer = 0
        Dim Degree As String = ChrW(176)

        For i = 1 To 20
            CurrentValue += Port.Read
            Thread.Sleep(10)
        Next i

        CurrentValue = CurrentValue / 20

        CurrentVoltage = CSng(CurrentValue * 3.3)

        CurrentVoltage = CurrentVoltage / 1024

        Temperature = CSng(CurrentVoltage - 0.5) * 100

        Fahrenheit = CInt((Temperature * 1.8) + 32.0) + TemperatureAdjustment

        CurrentTemperature = Fahrenheit.ToString




    Catch ex As Exception
        Debug.Print("Error in GetTemperature: " & ex.ToString)

    End Try

End Sub

End Class

To use just start the thread:
Tmp36.NewThread.Start()

Now the temperature can be read at any time using this.
Tmp36.CurrentTemperature

I hope this helps

1 Like

Hi David,
Thanks for replay, but TMP36 is analog sensor.
DS2401 is digital device.

Regards

ALEX

This example should help:

https://www.ghielectronics.com/docs/16/onewire

Someone wrote a driver for the DS18B20 a while ago. Discussion is here: http://forums.netduino.com/index.php?/topic/10762-1-wire-ds18b20-programming-errors/

Original code is here: https://www.ghielectronics.com/community/codeshare/entry/438

Could this be reused?

Regards,
Mark

1 Like

Strange , I read fine ds18b20 with onwire, but no work with ds2401.
There is a sample in comunity?

Regards
Alex