mcThings
Published © GPL3+

Wireless Handheld IoT Ultrasonic Distance Sensor

Just a quick showcase on a handheld, wireless and battery operated distance sensor. Incredibly long-life (8 + years!).

BeginnerFull instructions provided1 hour2,721
Wireless Handheld IoT Ultrasonic Distance Sensor

Things used in this project

Hardware components

mcModule120 Dev Kit
mcThings mcModule120 Dev Kit
This kit gives you everything you need to get going with mcThings. Not a requirement for this project however you will require at least one mcModule120, one mcGateway and one mcDongle
×1
mcMod120 Module
mcThings mcMod120 Module
You also require an mcGateway and mcDongle to bring the information from the device to the cloud. Consider an mcModule120 development kit!
×1
Hammond - Nice blue case with easy access to batteries
×1
US100 - Ultrasonic Sensor
You can get these from many different suppliers - search around for the best price from your preferred supplier
×1
Generic AA battery clip
×1

Software apps and online services

mcStudio
mcThings mcStudio
MQTT
MQTT

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

mcModule120 Product brief

mcModule120 Schematic

Code

US-100 Ultrasonic Sensor library

mcScript
Define PinMode Pin4 As DigitalOutput Alias sensorPower

Class US100 
    
    Const UART_BAUDRATE As Integer = 9600
    Const DISTANCE_COMMAND As Byte = 0x55
    
    Private _uartDev As Uart
    Private _txPin As Pin
    Private _rxPin As Pin
    Private _buf As ListOfByte
    
    Public Sub New(tx As Pin, rx As Pin)
        Disable()
        _txPin = tx
        _rxPin = rx
        _buf = New ListOfByte()
    End Sub
    
    Public Function GetDistance() As Integer
        _uartDev = Uart.Create(UART_BAUDRATE, _txPin, _rxPin)
        _buf.Clear()
        Enable()
        Thread.Sleep(800000)
        _uartDev.Write(DISTANCE_COMMAND) 'Command to request distance
        If Not WaitForResponse() Then
            _uartDev.Close()
            Disable()
            Return Nothing
        Else
            _uartDev.Close()
            Disable()
            'calculate and return distance
            Return _buf(0) * 256 + _buf(1)
        End If
    End Function
    
    Private Function WaitForResponse() As Boolean
        Dim try As Integer = 3000
        While (try > 0)
            If _buf.Count>= 2 Then
                Return True
            End If
            try -= 1
            Thread.Sleep(1000)
        End While
        Return False
    End Function 
    
    Public Sub Receive()
        Dim chr As Integer = _uartDev.Read()
        While chr >= 0
            _buf.AddByte(chr.ToByte)
            chr = _uartDev.Read()
        End While
    End Sub
    
    Public Sub Enable()
        sensorPower = True
    End Sub
    
    Public Sub Disable()
        sensorPower = False
    End Sub
    
End Class

mcModule120 programming

mcScript
    Shared usSensor As US100
    
    Shared Event Boot()
        usSensor = New US100(Pin.Pin2, Pin.Pin1)
    End Event
    
    Shared Event DistanceMeasurement() RaiseEvent Every 30 Seconds
        ' Create distance JSON object
        
        Dim distance As Integer = usSensor.GetDistance()
        Dim payload As Json = New Json  
        payload.Add("mmDistance", distance) ' distance in mm
        
      
        'Remove in final design (only used for debug)
        Dim payloadString As String = payload.ToString()
        Dim stringlength As Integer = payloadString.Length()
        
       
        
        Lplan.Publish(topic, payload.ToListOfByte)
        
        
        
    End Event
    
    Shared Event Uart0Receive()
        usSensor.Receive()
    End Event
    
End Class

Credits

mcThings

mcThings

17 projects • 70 followers
Use mcThings to quickly & easily create, test and deploy IoT solutions for industrial, business/commercial and individual needs!

Comments