Hello All,
TL;DR
You can use a separate GPIO's to switch a transistor that will switch the GND pin of the DS18B20 effectively turning each sensor on one at a time and reading its address. then assign it to a physical location in software. This should be a part of your setup method. This would prevent you from adding time to your loop reading from individual bus's. This eats up a lot of GPIO's so you can simply use a GPIO expansion such as the MCP23017 to accomplish the same.
This is a hardware/software hybrid solution. It requires a mixture of both in order to achieve desired results.
Parts Required:
- Arduino of choice (mine was not an arduino but an esp8266)
- Multiple DS18B20's
- A MCP23017 GPIO expansion chip
- Same number of transistors as sensors (2n 2222a)
- Same number of resistors as transistors (43k)
With the popularity of the DS18B20's still growing, I wanted to offer an answer that may better answer this question. The software methods above are decent and should be considered if it can solve your needs. You could just use separate GPIO pins for each sensor which would take forever to read causing your loop to be extremely inefficient, IF you have enough pins available. I battled this issue for a couple of days and read through Stack posts as well as Arduino forum posts and all affirmed the same things:
- You cannot get physical location of any 1wire device because, electrically they are not in a different location.
- The enumeration process in the Dallas Sensor library orders by value.
- There are software ways to handle this with intervention by manually (or with an output) changing the temp of the probe.
- Multiplexing is an option, but possibly overkill and could lead to more complex code.
- GPIO expansions such as the MCP23017 or similar cannot reliably read the sensors and reading requires heavy software modification.
- Other sensors (such as DS28EA00) are able to be chained and a sequence detection function used to get physical location in the chain. But, if a member of the chain fails the following members will be unreachable (like xmas lights).
The last of the above seemed like a winner, but the fact that the links depended on preceding links (without bypassing) was not acceptable for my application. Furthermore, there are not many readily available probes on the market with this chip.
I started mauling it over and insisted using the MCP23017 when it hit me... I don't need to read the sensor through it.. I just need to switch power to the sensors individually, effectively turning them on one by one and assigning them a physical location in software. A simple loop can take care of this.
This failed on my first attempt because driving the power pin LOW of the DS18B20 would Cause the rest of the sensors to also go low. I didn't go into great detail on why this happened, but I knew something had to change. But, when the power/ground pin of a sensor was floating, the rest of the sensors were unaffected.
So, I decided to tie the power pin to the supply, data pin to the 1wire pin, and use the MCP23017 to activate a transistor (acting as a switch) to switch the ground pin of the DS18B20. At that point I had accomplished what I set out to do. Read from a specific sensor to provide feedback for proportional control of a heat source. Or, ,to simply know the physical location of the sensor I was reading from so i could use it.
For those of you who are as excited as I was when I first got it working, and who may not have an idea how to get started... I have some useful information below, the rest of you are welcome...
I have no real idea if this information will be found useful, or if its just me, so I am not wasting time with diagrams or software examples right now. In essence, you switch each pin of the MCP23017 where you connected the transistor and sensor and assign it a location in software.
Things you will need:
- Arduino of choice (mine was not an arduino but an esp8266)
- Multiple DS18B20's
- A MCP23017 GPIO expansion chip
- Same number of transistors as sensors
- Same number of resistors as transistors
Well, how do you choose a transistor you might ask... That is extremely complicated to really explain to someone who isn't an EE, so here is what I used and how you can determine what you should use.
Transistors I used are 2n 2222a's
Resistors I used are 43k @ 1%
NOTE: Without a resistor, if the current inst regulated, you will destroy your transistor. You Should generally always include one anyways even if the current is limited.
DISCLAIMER: I am not an EE, so don't freakin bash me semantics. Politely correct me and have me edit the post.. thanks
When choosing a transistor for switching you will likely use an NPN type. You need to choose one with a decently high minimum current gain (hfe). The higher the minimum current gain the less current you will need to switch the base junction and allow current to flow from the collector to the emitter. In my opinion 100 to 400 hfe is sufficient. You will need to gather the following info to then calculate the resistor:
- The max current of the load you will be switching. DS18B20's are 1.5a
- The max current gain of the transistor you are looking to use.
- Voltage that will be supplied to the base pin of the transistor.
Then you need to make the following calculations:
- Determine the current needed on the base to allow the amount of current required for your load to flow from collector to emitter. (loadCurrent / hfe) In my case it is 1.5ma / 100 = .015ma.
- Give yourself some wiggle room.. I chose to round up to .02ma.
- Determine the resistance needed. (voltage - drop across PN gate) / amps required to switch = resistance. In most transistors the voltage drop is .7v which i suppose is the magic number. Also, convert your miliamps to amps. In my case it is (5v - .7v) / .00002a = 215,000ohms
Now, that is a pretty heft resistor in my case, so I wanted to make a bit of an adjustment. The GPIO pins of my device can handle 12ma so, even if somehow I drew that much from maybe an external supply, I would be safe bumping up the current gain a bit. I worked backwards to get smaller and cheaper resistor of 43k.
10ma / 100hfe = .1ma = .0001a
(5v - .7v) / .0001a = 43,000ohms
This will allow me to supply 5v through the 43k resistor at .1ma to the base pin of the NPN transistor which in turn, allows up to 10ma of power to flow from the collector pin to the emitter pin.
This transistor setup will work for all sorts of stuff (as long as it draws less than 10ma) for my device. LED's, Sensors, small peripherals. The only thing I can note here is, if you intend on using it with a solenoid or other conductor (coil), you will need to place a "flyback diode" around the coil to avoid high voltage spikes that will destroy the transistor when the EMF collapses.
Hope this helps somebody out on a few different levels! And if anyone wants a wiring diagram or would like me to expand on this, feel free to ask.
SIDEBAR: I am a bit disgusted at a lot of the responses I see to members of this forum, and it has discouraged me from actually posting questions. I see posts that are a bit malformed or incorrect, where members (Brattain included) will be so condescending using snark remarks and very short answers with little to no value. I run a couple of forums, I get it. But, when your demographic is driven towards enthusiasts and the questions are NOT coming from EE's, why is there a lack of willingness to provide actual help. Why are responses like "Why are you trying to do it this way, that's not right, you should be using this verbiage instead of that word, blah, blah, blah" even allowed?
I can understand there is a need to correct someone who misunderstands a concept, or lead them in the right direction by having them rethink their approach, but some comments just blow my mind. And a lot of times it is simply preference. There are also wild claims that are blatantly incorrect, sometimes telling people that something cannot be done and it really can if it is just thought about differently. (maybe such as this post) This forum should promote creativity, not destroy it. END RANT
cheers