Page 1 of 1

PhidgetSBC4 ID: SBC3003_0 program problem

Posted: Thu Feb 25, 2021 9:53 am
by nmadonia
I am new to programming of this type, usually I program in python but not electrical components.
Since I am the only one in my office who program in python, they asked me to program a device that turns on and off a fan for cooling the Peltier cells.
The components at my disposal are:
1 PhidgetSBC4 ID: SBC3003_0;
1VINT Hub Phidget ID: HUB0000_0
1 Humidity Phidget ID: HUM1001_0
1 12V battery pack
Peltier cells
1 Brush Motor Control CC 1000_0
1 12V fan

The connection should thus be SBC 3003_0 powered by the 15V battery pack, it receives as input from the HUB 0000_0 the temperature and humidity data which it receives in turn from the HUM 1001_0.
Depending on the temperature and humidity detected, the Brush Motor Control Fan CC 1000_0 must turn on.

The code I wrote (copied from your site) is:

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
from Phidget22.Devices.DigitalOutput import *
from Phidget22.Devices.HumiditySensor import *
from Phidget22.Devices.TemperatureSensor import *
import time

#Declare any event handlers here. These will be called every time the associated event occurs.

def onStateChange(self, state):
	print("State: " + str(state))

def main():
	#Create your Phidget channels
	digitalInput0 = DigitalInput()
	digitalOutput4 = DigitalOutput()
	digitalOutput5 = DigitalOutput()

	#Set addressing parameters to specify which channel to open (if 
        #any)
	digitalInput0.setIsHubPortDevice(True)
	digitalInput0.setHubPort(0)
	digitalInput0.setDeviceSerialNumber(1000)
	digitalOutput4.setIsHubPortDevice(True)
	digitalOutput4.setHubPort(4)
	digitalOutput4.setDeviceSerialNumber(1000)
	digitalOutput5.setIsHubPortDevice(True)
	digitalOutput5.setHubPort(5)
	digitalOutput5.setDeviceSerialNumber(1000)

	#Assign any event handlers you need before calling open so that no 
        #events are missed.
	digitalInput0.setOnStateChangeHandler(onStateChange)

	#Open your Phidgets and wait for attachment
	digitalInput0.openWaitForAttachment(5000)
	digitalOutput4.openWaitForAttachment(5000)
	digitalOutput5.openWaitForAttachment(5000)

	#Do stuff with your Phidgets here or in your event handlers.
        humiditySensor0 = HumiditySensor()[/b]
        temperatureSensor0 = TemperatureSensor()
        if digitalInput0_temperature > """write the temperature max""" or
           digitalInput0_humidity > """write the humidity max""":

        
    
        digitalOutput4.setDutyCycle(1)
	digitalOutput5.setDutyCycle(1)

	time.sleep(5)

	#Close your Phidgets once the program is done.
	digitalInput0.close()
	digitalOutput4.close()
	digitalOutput5.close()

main()

I use Python 3.7 and Spyder as a compiler.
humiditySensor0 is considered an error by Spyder.
But my question is how do I import the temperature and humidity from digitalInput0?
The digitalOutput4 and digitalOutput5 ports are set to True is this correct, or should they be set to false and then in the if set to True when the temperature or humidity reaches a certain threshold?
If the loop has to repeat itself "infinite" times do I have to write digitalInput0.close ()?
I thank you in advance. Sorry for my silly questions, but I just don't know how.

Regards
Nicola

Re: PhidgetSBC4 ID: SBC3003_0 program problem

Posted: Thu Feb 25, 2021 10:36 am
by mparadis
Looking at your code, I think there are a lot of misunderstandings:

- for setDeviceSerialNumber, you're using the number 1000 but the serial number is usually a six-digit number. Your HUB0000 and SBC3003 both have serial numbers written on the sticker, and setting the serial number for a channel just tells your program which hub to look at in order to find that channel.

- Based on your description of what you want to have happen (when temperature and humidity exceeds a certain threshold, turn on the fan) you shouldn't need digitalInput and digitalOutput objects. A digitalInput reads a 0-5V signal and returns 'true' when it's above ~2.5V and 'false' when it's below that. A digitalOutput is a pin on the board that will be 0V or 5V depending on the value you give it in your program. For your program, the only objects you will need are HumiditySensor(), TemperatureSensor(), and DCMotor().

- In your code, you didn't open the HumiditySensor or TemperatureSensor objects. Also, "digitalInput0_temperature" and "digitalInput0_humidity" are not valid properties (as mentioned above, digital inputs and outputs are separate from the temperature and humidity objects).

To answer your questions:

- As mentioned above, temperature and humidity are their own objects and not accessed through digital inputs.

- Setting the digital outputs to true simply set the data pins of those hub ports to 5V, useful for turning on an LED or other simple circuit.

- This program will not loop infinitely- it will close once main() has finished. If you want it to loop forever, you'll have to use a 'for' or 'while' loop. You should still close the channels after the loop, and it would be useful to have a way to exit the loop by pressing a key on the keyboard.


Here's how I would approach writing this program:

1. Download the python examples for the HUM1001 and the DCC1000. Go here and select those devices and copy the code into separate .py files. When you download the HUM1001 example make sure both the "Humidity Sensor" and "Temperature Sensor" boxes are checked so you get both objects in the code sample.

2. Run each code sample separately to make sure each device is working. The HUM1001 example will print temperature and humidity values to the screen until you press a key. The DCC1000 example will set your fan to full speed until you stop it by pressing a key.

3. Start editing the HUM1001 example- In the main function where the objects are defined and opened, also open a DCMotor() object in the same way it's done in the DCC1000 example.

Then,instead of printing the humidity and temperature to the screen in the event handlers, set the value of a global temperature and global humidity variable. In your main function, after opening the channels, make a infinite loop with an 'if' statement that constantly checks these global variables. If they're both above your chosen threshold, set the targetVelocity of the DCMotor object to a non-zero value (1 is full speed, 0.5 is roughly half-speed). You'll also add an 'else' that sets the targetVelocity to 0 if the temperature or humidity is below threshold.

4. Once you've tested this and it's working, you can implement more advanced fan control by setting targetVelocity to values in between 0 and 1. This way you can turn the fan on gently when the temperature climbs instead of waiting until it needs to be turned on full-blast.

I hope that helps, let me know if you run into any problems along the way.

Re: PhidgetSBC4 ID: SBC3003_0 program problem

Posted: Fri Feb 26, 2021 4:28 am
by nmadonia
Thanks for the thorough answer.
I will test the script and components asap and let you know.
Regards
Nicola