Design Review: Embedded Beginner - Temperature Reading Script

This document provides a design review for the MicroPython script PWAM100/tempds18x20.py, which reads temperature data from a DS18B20 sensor connected to a Raspberry Pi 2W.

1. General Comments

The script is well-structured for a beginner and achieves its goal of reading temperature data. The comments are helpful in understanding the code. The use of descriptive variable names like dstemp is good.

2. Code Structure and Readability

2.1. Imports

import ds18x20, onewire from machine import Pin from time import sleep, sleep_ms

The imports are clear and bring in the necessary modules.

2.2. LED Initialization

led=Pin("LED",Pin.OUT) led.value(1)

Initializing the LED and turning it on initially provides a visual indication that the script has started.

Consider adding a comment explaining the purpose of the LED (e.g., "Indicates script is running").

2.3. OneWire and DS18B20 Initialization

ow = onewire.OneWire(Pin(1)) # sensor connected GPIO01 ds = ds18x20.DS18X20(ow) roms = ds.scan()

The initialization of the OneWire bus and the DS18X20 sensor is done correctly. The comment indicating the GPIO pin is very helpful.

It might be good to add a check after ds.scan() to see if any sensors were found and handle the case where no sensors are detected.

2.4. Main Loop

while True: ds.convert_temp() sleep_ms(750) dstemp=ds.read_temp(roms[0]) print('{"dstemp":'+str(dstemp)+'}') led.toggle() sleep_ms(5000)

The main loop reads the temperature, prints it in JSON format, and toggles the LED. The delays seem reasonable.

Consider adding error handling around the temperature reading. For example, what happens if the sensor reading fails?

If multiple sensors were connected (and detected in roms), the script currently only reads the temperature from the first one (roms[0]). If the intention is to support multiple sensors, the loop should iterate through the roms list.

The 750ms delay after convert_temp() is generally recommended to allow the conversion to complete. Check the DS18B20 datasheet for precise timing requirements if needed.

The 5000ms (5-second) delay between readings might be adjusted based on the application's needs. For testing, a shorter interval might be preferable.

Printing directly to the console is fine for basic applications or debugging. For more robust applications, consider logging to a file or sending data over a network.

2.5. Connection Information

# connection # DS18B20 ''' 1 DAT GP01 2 3 VDD 3V3 4 GND GND '''

Providing the connection information is crucial for anyone trying to replicate this setup.

It might be helpful to explicitly mention any pull-up resistor requirements for the data line (GP01) if necessary for the DS18B20 to function correctly.

3. Potential Improvements for Beginners

4. Specific Suggestions

5. Conclusion

The script is a good starting point for reading temperature data with a DS18B20 sensor on a Raspberry Pi using MicroPython. By incorporating the suggested improvements, particularly around error handling and potential support for multiple sensors, the script can be made more robust and versatile for embedded beginners.