In this project, Digilent Pmods and Raspberry Pi 4 are used to measure the temperature, control the speed of a DC motor, and display data. Digilent Pmod HAT connects Pmod TC1 Thermocouple, Pmod HB3 H-Bridge Motor Driver, and Pmod OLEDrgb OLED Display to Raspberry Pi 4. The motor controller will have two modes: one in which the motor speed changes according to the temperature (in this mode temperature limits for 0% and 100% motor speed can be set on the user interface) and a second mode, in which the motor runs with a constant speed for a defined time, then it is stopped for the same time (in this mode, the motor speed and the time are the parameters controllable by the user). The user interface will be used to change modes.
Both Pmod OLEDrgb and Pmod TC1 (with SPI interface) can only be connected to specific ports on Pmod HAT. Pmod OLEDrgb is connected to port JA while Pmod TC1 can only be connected to port JBA. Pmod KYPD is connected to port JC and the Pmod HB3 is to port JBB. Pmod TPH2 can be used to break out port JB so that the two Pmods (TC1 and HB3) can be connected to the same port. Connect the thermocouple wire and the motor to the respective terminal blocks. As mentioned above, you can power the motor directly from the Raspberry Pi. The attached Fritzing file shows the connection.
The Python script consists of three modules: fan_controller.py, keypad.py and display.py. The fan_controller.py is the main module.
The fan_controller.py is the main module. The four functions are action_M1(), which sets the motor speed according to the measured and averaged temperature, action_M2(), which the motor speed is set, then the function waits for the defined time (keypresses finish the function). The function set_param() sets a parameter. It contains the main loop, in which keypresses are checked. If no key is pressed, the action of the current mode is executed. If a key is pressed, the motor is stopped and the key is decoded, the respective menu is displayed. When exiting the script, proper cleanup is necessary. r and the function decode_key() decides which menu, or submenu to display on a keypress.
The module consists of an empty object (KYPD) which is initialized in the fan_controller.py, an exception type when a key is pressed (in mode 2). There are 4 functions: get_param() sets a new keymap and map letters to 0 and reads a two-digit number from the keypad; wait_for_key() is the debounced, blocking key reader function; get_key() is the debounced, non-blocking key reader; debounce() return every keypress only once. To return a keypress, the key must be released and is checked by validating 1000 consecutive “None” states of the keypad.
The display module has an empty object (OLED) which is initialized in the fan_controller.py, and 7 functions to display various data. These functions start with a line, clear the screen, and draw multiple lines of text.
Test the system
Initially, the speed of the motor is set according to the temperature. Press any key to access the main menu. In the main menu, choose mode 1 (press A) or 2 (press B). When you are in one of the modes, you can set parameters by pressing C or D. You can exit the menus by pressing any key (which is not listed in the menu or submenu).
In mode 1, the speed of the motor depends on the temperature. Users can set the temperature limits for 0% and 100% speed from the submenu. In mode 2, the speed of the motor is constant for a predefined time (initially 15 minutes). Then, the motor stops for the same amount of time. Users can set the motor speed and the time in mode 2.
Project details and sources are available at https://www.element14.com/community/groups/embedded/blog/2021/04/22/fan-controller-with-raspberry-pi-and-pmods