![]() |
Forum Index : Microcontroller and PC projects : Python Serial On Pi Pico
Author | Message | ||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hello all, I am trying to use my VL53L1X laser distance sensor on a Pi Pico. As mentioned (here) I can get it working with Python (supplied with the sensor) but I can't see how to port the Python code to MMBasic (that would be my preference). If anyone is interested I can post the Py code, datasheet etc? (please . . . ?). A second way I have tried is to modify the Py code to output the data to an HC12 (I am reasonably familiar with them). I have copied Lew247's code where he does the same thing. But I get an error. This is my code snippet: from PiicoDev_VL53L1X import PiicoDev_VL53L1X from time import sleep import serial ser = serial.Serial( port='/dev/ttyAMA0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) distSensor = PiicoDev_VL53L1X() while True: dist = distSensor.read() # read the distance in millimetres print(str(dist) + " mm") # convert the number to a string and print sleep(1) and the error I get: >>> %Run -c $EDITOR_CONTENT Traceback (most recent call last): File "<stdin>", line 4, in <module> ImportError: no module named 'serial' >>> My intention is that when I can successfully open the port and test it, I'll insert code in the while loop to write to the port. Some questions: - is there a subroutine or library that I need to load for serial (if so where is it and how do I load it?) - how does one write to the port (its not as simple as PRINT #1, "Hello World" is it?) - how does one specify which Com port on the Pico to use? (I'm happy with Com1 on GP0 and GP1 but can use another). Many thanks, Andrew |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 328 |
Looking at the docs for the sensor, it say the device is I2C. Check the Picomite docs for the proper I2C pin connections and commands. I believe the manufacturer of sensor expects you to only use their C API and provides no other information on how to access the sensor. I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Thanks Curtis, Yes it is I2C. I bought the module from Core Electronics (in Aus.) and they supply the following Python code: Piico Software.zip This is what I can successfully run. It uses Pins GP08 & GP09 for SDA and SCL. I'd dearly like to port that code to MMBASIC. The snippet of code above is 'MAIN.PY' in the zip. If I can get serial to work I hope to modify Main to write the data to an HC12. (I can't see any way of getting their C API (I've looked for days). I wouldn't know how to use it either . . .). Thanks again, Andrew |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 328 |
I have PMed some info to you. Hope it helps. I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
This one looks pretty cool :) |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Justplayin, This is my second attempt to thank you. Thanks. I'll report back when I can test your suggestions. Cheers, Andrew |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
JustPlayin, Thanks for the suggestions but no joy yet. But I did find a link to ST's API here.(Its in C so that will make it harder.) It appears to be the basis of the Python code I've been looking at (it has the same comments) so I will see how I go in MMBASIC. Andrew |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 328 |
Oh well... Sorry it didn't help. --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
@Andrew_G Same device but packaged and with a serial ASCII mode |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hi Tinine, Many thanks and a Happy New Year to you. Well spotted! I've ordered one but who know what and when it will turn up ... (I reckon that the bottle of decent Aussie. red that I'm about to finish is about the same cost, Pepperjack Cab. Sav.). I'm still keen to get the existing one working because I have uses for both. Thanks again, Andrew |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
@Andrew_G Heck, that vino rosso idea has my mouth watering. ![]() My one and only attempt at drinking during this holiday was a 6-hour visit to the pub but I went, armed with an Android tablet and a W10 tablet ![]() Only got through 3 pints ![]() Wine, cheese and crackers might be the order of the day ![]() |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7938 |
I managed a whole 2 glasses of wine - on different days. Cheap to keep, me. Keep taking the tablets. ;) Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
I lived in Cheadle (Cheshire) for three years (in my late 20s). I managed a pint of Guinness at each of its eight pubs - walking only, a little awkwardly at the end! Arrgh - a misspent youth, I didn't drink until I was 21. Cheers, Andrew |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
G'day all, This is only a quick report as I'm rushing to get out of town for a few days BUT! My device arrived and it works a treat! It is labelled as a "VL53L-400" and described as a "WT53D" There are links to WitMotion's PC software or connections to the Picomite for either I2C or Serial. It took me 10 minutes to get the serial working (at 115200 baud). In that mode it just spits out readings continuously. They seem about right but I havnt confirmed their accuracy yet. My next steps are to send it to an HC12 every x seconds and to test it over water. More info in a day or three but I'll try to answer questions in the meantime. (I had wasted a heap of time unsuccessfully trying to get Python to do serial or to get MMBasic to talk to my original VL53L1X - I'm happy with my recent purchase). Cheers, Andrew |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
@Andrew_G ![]() ![]() ![]() I was hoping that you would report back. Definitely keen to learn more about this device. Would be even better if you could make vids of your experiences. Cheers, Craig |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
It's been a bit more than "a day or three" ![]() I think I might be ordering a few of these things ![]() Craig |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hi Craig, Sorry about the delay but I do have good excuses. No vids but it really is trivial to get them working and measuring distances and reporting via serial on HC12 on a MMBasic on a Picomite (or I guess many others too). I have two units but will get more when I've sorted my life out. I can't do much else at present: - all my gear is packed into boxes for removalists - we are due to demolish this house in a few weeks but don't yet have anywhere for us or the gear - still changing house design for signing contracts in a week or two - . . . Andrew Edited 2022-02-12 16:56 by Andrew_G |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Craig et al, Here is my code for what it is worth: 'Pico@Wit TBS.BAS 'Uses Com1 to talk to a WitMotion V53L laser ditance sensor 'Com2 writes to an HC12, with a checksum should you wish. ' 'Connections: 'Pico V53L ' 3V3 to Vcc ' GND to GND ' GP0 to RXD (Green) ' GP1 to TXD (Yellow) ' SCL n/c ' SDA n/c ' 'Pico HC12 ' 3v3 to Vcc ' GND to GND ' GP4 to RXD ' GP5 to TxD ' SET n/c ' 'option Explicit 'zzz next job 'Option Autorun ON 'best done from the Console Dim String Dat, Range, In_Char, Out, Message Dim Integer OK, Rng_Ave, Ave_Count = 5, Count 'Flag set if "Range Valid" (VALID is an MMBasic keyword) Dim Integer Range_Start, Range_End, Chksum Dim Integer Range_Freq = 4000 'Frequency of getting Range data m_seconds SETPIN GP0, GP1, COM1 'Assign the I/O pins for the first serial port for V53L setpin GP4, GP5, COM2 ' I/O pins for the HC12 OPEN "COM1:115200" AS #1 'open the first serial port (V53L default speed is 115200) open "COM2:9600" as #2 'open the second serial port with a speed of 9600 baud (for my HC12 network) Send_Message "Starting" SETTICK Range_Freq, Get_Range, 1 'Get a range every 'Get_Range' m_seconds - change to suit do'zzz Do something useful in here? Watchdog Range_Freq * 3 'If program is hung start again Loop CLOSE #1 end Sub Send_Message(Txt$) Txt$ = Txt$ + "," + time$ + "," + Date$ Print Txt$ Out = "$AGRNG," + Txt$ 'These are my NMEA-like sentences Out = CRC$(Out) print #2, Out End Sub 'Send_Message Sub Get_Range Local Integer OK = 0, Count = 0, Rng_Ave = 0 Watchdog Range_Freq * 3 'All other variables are Global Timer = 0 do Dat = "" If Timer > Range_Freq * 0.9 then Send_Message "Quitting Sub" Exit Do ' End If do IF LOC(#1) > 0 THEN In_Char = INPUT$(1,#1) ' Do we have a LF or CR character = end of line? If asc(mid$(In_Char,len(In_Char),1))=10 or asc(mid$(In_Char,len(In_Char),1))=13) then Exit do Else Dat = Dat + In_Char end if END IF loop If Instr(Dat, "Range Valid") then If Not OK then OK = 1 'This will hold for the duration of the Sub End if End If if Instr( Dat, "Signal Fail") then OK = 0: Send_Message "Signal Fail" if Instr( Dat, "No Update") then OK = 0: Send_Message "No Update" If OK then 'This will be OK for subsequent reads until next time period. It will then start as false Range_Start = Instr(Dat,"d:") Range_End = Instr(Dat," mm") If Range_Start >0 and Range_End >0 then 'We have a valid range (it has both a Start and an End) Range = mid$(Dat, 1, Range_End) Range = mid$(Range, Range_Start+2) Range = Trim$(Range, " ") If Count < Ave_Count then Rng_Ave = Rng_Ave + vaL(Range) Count = Count + 1 'ZZ use to count valid Ranges for average print Range, Count Else Rng_Ave = Rng_Ave / Count Range = str$(Rng_Ave) Out = "$AGRNG,"+ Range Out = CRC$(Out) print "Ave = "; Range; tab(15);Out$ print #2, Out OK = 0 Exit Do End if Else End If End if loop End Sub 'Get_Range ' trim any characters in c$ from the start and end of s$ Function Trim$(s$, c$) Trim$ = RTrim$(LTrim$(s$, c$), c$) End Function ' trim any characters in c$ from the end of s$ Function RTrim$(s$, c$) RTrim$ = s$ Do While Instr(c$, Right$(RTrim$, 1)) RTrim$ = Mid$(RTrim$, 1, Len(RTrim$) - 1) Loop End Function ' trim any characters in c$ from the start of s$ Function LTrim$(s$, c$) LTrim$ = s$ Do While Instr(c$, Left$(LTrim$, 1)) LTrim$ = Mid$(LTrim$, 2) Loop End Function FUNCTION CRC$(Txt$ AS String) 'Returns the Txt$ passed to it with "*hh" added, where hh is the NMEA Hex checksum 'The receiving station checks the checksum - if not OK it just ignores that sentence 'Local String Txt$ Local Integer n Chksum = 0 FOR n = 2 TO LEN(Txt$) 'n=2 makes it skip the leading $ Chksum=Chksum XOR ASC(MID$(Txt$,n,1)) NEXT n CRC$=Txt$+"*"+HEX$(Chksum,2) END FUNCTION Any comments/suggestions/questions welcome, Cheers, Andrew |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |