Tags:
create new tag
view all tags

Exercises in Python

One way to develop the course in a new direction is the replacement of the C programming language by Python. You should not be mislead however by thinking that this will make things substantially easier. In fact the string typing available in C helps you in many situations e.g. when reading out unsigned or signed char or short values from sensor registers, which may lead to strange effects when doing the same thing in Python.

To start you off, I have already re-written the exercise solutions for the LEDS, and the devices that make up the meteorological station:

  • Hitachi HD44780 LCD display controller
  • the DHT11 humidity and temperature sensor
  • the BMP180 temperature and air pressure sensor
The code is attached to the bottom of this page. Here are proposals on how to continue:

First you may try to re-write the stepping motor exercises, which can be treated as 4 LEDs. Reading the Python code of the LED exercise solution should give you a reasonably good idea on what to do for the stepper motors.

Then you may want to pick up the pieces of the weather station and complete this example. The driver software for the individual sensors are maybe not entirely complete (reading back the text from the HD44780 is not implemented yet) but everything needed for the weather station is available.

The HD44780 driver

All sensor drivers are implemented as Python classes and you must create an instance of the corresponding class. The file of the driver is called lcdDisplayClass and the class name is lcdDisplayClass as well.

lcdInstance.png

When you create an instance of the LCD driver class the connection to the pigpiod daemon is established and the Hitachi hd44780 is fully initialized. After this the device is ready to be used.

These are the methods of the class:

  • setBacklight (onOff): onOff is a boolean which allows to switch the LCD back light switching on or off
  • putC(c): put the character c onto the screen at the current cursor position
  • putS(s): put the string s onto the screen
  • readBusy(): reads the current ddram address (lower 7 bits) and the busy flag (bit 8)
  • setDDramAddress(address): set the ddram pointer to address
  • secondLine(): switches the ddram address to the second line of the display
  • display(onOff): onOff is a boolean which switches the display on or off
  • home(): sets the ddram address to 0
  • close(): closes the connection to the display
helloWorld.py shows you an example on how to use the class.

The DHT11

The DHT11 measures the temperature and the ambient humidity.

The file with the python class is called dht11Class and the class Dht11. To instantiate the class you must pass the gpio PIN number to which the DHT11 is connected to.

dht11instance.png

Here are the methods of the DHT11 class:

  • measure(): takes and measurement
  • getTemperature(): returns the temperature but without checksum test
  • getHumidity(): returns the humidity but without checksum test
  • getTimeStamp(): get the time stamp of the last measurement with checksum test
  • getValidatedTemperature(): same as getTemperature but with successful checksum test
  • getValidateHumidity(): same as getHumidity but with successful checksum test
  • getValidTimeStamp(): same as getTimeStamp but with successful checksum test
  • getValidTimeString(): the time stamp converted into a human readable form
dht11Example.py is example code showing how to use the dht11 class.

The BMP180 air pressure and temperature sensor

The class in implemented in the file Bmp180Class.py and its name is Bmp180Class.

Here are the methods:

  • chipId(): returns the chip ID, which is always 0x55. This shows you that the bmp180 can be correctly accessed.
  • setResolution(oss): oss = 0..3. Please check the possible resolutions in the BMP180 data sheet
  • getResolutions(): returns the currently set resolution
  • measure(): takes a measurement
  • getTemperature(): returns the temperature on C with a precision of 0.1 C
  • getPressure(): returns the air pressure in hPa
Again bmp180Example.py shows how to use the class.

-- Uli Raich - 2017-12-04

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatgz bmp180Python.tar.gz r1 manage 3.1 K 2017-12-04 - 08:45 UnknownUser  
Unknown file formatgz dht11Python.tar.gz r1 manage 45.5 K 2017-12-04 - 08:48 UnknownUser  
PNGpng dht11instance.png r1 manage 16.8 K 2017-12-04 - 10:02 UnknownUser  
PNGpng lcdInstance.png r1 manage 20.1 K 2017-12-04 - 09:25 UnknownUser  
Unknown file formatgz lcdPython.tar.gz r1 manage 9.5 K 2017-12-04 - 08:47 UnknownUser  
Topic revision: r1 - 2017-12-04 - uli
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback