I try to develop a pretty complete meteorological station based on Arduino Mega2560. I included a wide variety of sensors, but now i require many interrupt pins...
Let me explain what sensors i have:
TSL230R to measure irradiance (requires 1 interrupt pin)
DH21 to measure air temp/hum
BMP085 to measure atmospheric pressure (use SDA and SCL pins, what in arduino mega 2560 are interrupt pins)
QRD1114 to measure wind speed (requires 1 interrupt pin)
Rain sensor (requires 1 interrupt pin)
Rain gauge (requires 1 interrupt pin)
Waterflow sensor to measure the consume on irrigation water (requires 1 interrupt pin)
Water level sensor to prevent the irrigation water deposit to be empty (requires 1 interrupt pin)
Device wake up button to check the device bythe user (i do not use LCD of things like that) (requires 1 interrupt pin)
RTC alarm to wake up automatically the station to measure the parameters (requires 1 interrupt pin)
So, this complete station requires 9 interrupt pins, but Arduino mega2560 "only" have 6, but two of them are already in use for the I2C communications with the barometers and the RTC, and one more on capture the alarms from the RTC. So, only 3 free what i could use for 3 of the sensors, but i still need other 3 more interrupt pins.
I know the "Pinchangeint" library, but i do not completely understand it,and i do not know how to apply to arduino mega, because the examples are for arduino uno board. So, i don´t know which pins to use for what sensor (and other electronic components not explained here such a rgb led, bluetooth communications,...). I neither know if i could use at the same time normal interrupts (typical attachinterrupt)and interrupts managed by this library..
So, i am really lost, because all the sensors work perfectly individually, but i don´t know how to solder all toguether into an shield because i don´t know to which pin should i use and how to write the code respect the interrupts.
Could you give me some ideas and help?
Thanks so much!
As you say, the windspeed sensor take care about the frequency, but also other sensor, such as the irradiance sensor and the waterflow sensor. The rain gauge i do not thing it will realy require measure the frecuency because is should be so low, (a couple of interrepts per second in the extreme case, i think).
For the other elements, I only need to capture the interrupt to wake up the device, or to discovred that something happened: (the user wnat to irrigate the cropfield, or to check the status of the device and the battery, or toknow when the rain started, or things like that.
@Robin2, What do you have on mind when you say that i should rethink the project? Thanks!!
Edited:
Ok, an extra information: i want to take the arduino sleeping for low power consume since it will be an stand-alone device powered by battery, a power regulator and a solar cell. Then, what i have on mind is to send the arduino to sleep between each measurement period (one hour or 30 minutes, depending of the user requirements), wake up, measure the sensor, decice if the crop requires irrigation or not (start the irrigation if required), and then go to sleep without take care of anything except the alarm interrupt, the 2 buttons (irrigation and test), and water level interrupt to stop the irrigation in case the water deposit is near to be empty (for not to damage the water pump).
It's fine to use interrupts for the wind speed and water flow. You should not be using interrupts for any of your other sensors. You deal with them by polling.
@holmes4 Thanks for your reply.
However, the irradiance sensor also require frequency measurement, so i need an interrupt pin. At least this is what is deduced from the datasheet, andthe different codes i found in internet.
In any case, i am not an expert, so, if not using interrupts... How should i manage them? I am really lost.
Thanks!!
Note that a lot of libraries (in concreto my DHTlib) do not disable interrupts so reading a value can be disrupted.
The same is true for I2C modules.
Two tips:
Keep interrupts as short as possible (only increment a volatile long counter) and use interrupts() and noInterrupts() functions to postpone the processing of interrupts.
Note that a lot of libraries (in concreto my DHTlib) do not disable interrupts so reading a value can be disrupted.
The same is true for I2C modules.
Yes, i use your library (BTW, thanks so much!) but DH21 sensor is not connectet to an interrupt, but what i understand from your comment is that i still could use pins 20 and 21 of the arduino to read the alarms, for examen of the buttons altough they are also used by the RTC and the BMP085 sensors, right?
Then, i could connect I2C communications ports of the RTC and BMP0085 sensor to pins 20 and 21, and at the same time connect to those pins each one of the buttons... then, when the arduino is sleeping, the interrupt could be ready to hear from the buttons. If one of the buttons is used, i should disactivate the interrupts to allow RTC and/or BMP085 sensors to send their data without the disturbance of the interrupts... Am i right?
If so, this is great, because it means that i need less extra interrupts...
What I meant to say was that the communication of the DHT library can be corrupted by interrupt code that takes too long.
Mind you, interrupts can come in in the middle of the transport of the measurement between sensor and CPU.
Okay, i see. But when the device will be sleeping, it will be taking care of the interrupts (buttons and alarm). When it get up, i could disable the alarms and interrupts, power the sensor by an 4n35 optocoupler, read the DH11 and BMP085 sensors, and then to activate the interrupt required to measure the others sensors.... In that case, when the interrupt will rise the arduino, i don´t care about the DH11 and BMP085 or RTC data, because their data were already saved... At the end of the measurements, i power down again the sensors and active the interrupts from buttons and alarms.
Does it have more sense? Does it avoid the problem you presented?
In any case, with this scenario i presented in the previous post, could i share interrupt pins with I2C ?
If your problem is that you want multiple sources to be able to generate an interrupt to wake the Arduino, perhaps you could combine them electrically (i.e. by connecting each input via a diode to the same pin) that that an active signal on any of them will trigger the interrupt. If you also connect each signal directly to a separate I/O pin (doesn't need to be interrupt capable) then you can read the input states to see which one(s) are active.
perhaps you could combine them electrically (i.e. by connecting each input via a diode to the same pin) that that an active signal on any of them will trigger the interrupt.
Yes, by this method, i could combine, for example, a sensor and a button... When the device is sleeping, the only cause of the interrupt is the button in that pin... and during the measurement, i only receive data from the sensor and not from the button.
Thanks for call my attention about the diode.
If you also connect each signal directly to a separate I/O pin (doesn't need to be interrupt capable) then you can read the input states to see which one(s) are active.
Yes, but in this case i need to wake up the sensor continuosly to check which one is active and which one not... May be is not the better option, right?
Following the first idea, may be i could organize the interrupt pins in this way:
Pin Use
2 RTC Alarm / Waterflow sensor
3 Irradiance (TSL235)
18 Rain gauge
19 Windspeed / Water Level
20 I2C BMP085 & RTC / Button 1
21 I2C BMP085 & RTC / Button 2
I still need to know what to do with the Rain sensor
BTW, which kind of diode should i use? 1N4001 or Zener 5v1 ? Other?
madepablo:
Yes, but in this case i need to wake up the sensor continuosly to check which one is active and which one not... May be is not the better option, right?
I don't mean it's a different option, I mean it's part of the same solution. You would use the combined input to trigger the interrupt, and when the interrupt occurs the Arduino would read the individual pins to see which one caused the interrupt. Handling it from then on is conceptually the same as if you had a separate interrupt per device.
Ok, may be i didn´t explained all the required information...
The device will measure only once per hour... So, it will not be tracking the wind continuosly, but only at selected times when it wil be up. I don´t care about to measure wind speed, irradiation, or any other data in between. For that reason, i don´t need to take care about the interrupts from the sensors. In fact, they are "disconnected" because i will power the sensors by an optocoupler only during the o'clock times to take readings.... and then they will be disconnected again. Then, the unique elements what could provide an interrupt will be the buttons (to test the device or to start the irrigation by the user), the alarm from the RTC; or an interrupt from the water level to say that the water deposit is empty.
Then, i do not measure the level of the water... the sensor is near the bottom, and it just only launch an interrupt if the level is low. I need to measure the water flow to know the real consumeof water, because i dont know if theuser will fill the tank completely or not each time... so, what i measure is the water used in irrigation.
About the windspeed, it is an IR emitter-receiver sensor (QRD1114), such as those used in rotary encoders in robots. The anemometer moves a disc withblack/white fields, and the sensors return, high/low values what are captured by the interrup pin. Depending of the number of interrupts uring the measurement period, i could calculate the wind speed.
Sorry @PeterH and @Robin2. I see that i didn´t provideallthenecessary information.. SInce i have my mind on this issue for some time, i have all the ideas on my mind, but i din´t wrote them here. Sorry! And Thanks so much for your patience and interest on help me with this. I really appreciate it.
If you wake the Arduino once an hour then I can't see any need to use interrupts other than to collect data from the windspeed encoder or, perhaps, from something similar for water flow.
I suspect one pulse per revolution of the windspeed and waterflow sensors would be perfectly adequate and could probably be read perfectly well by polling - however if you know what you are doing interrupts would be simpler (if you dont, NOT).
Buttons certainly don't need interrupts.
What is the alarm from the RTC?
What sort of water flow meter have you? How many litres per minute? How accurate do you need to be?
Yes, the anemometer is not working perfectly in an arduino uno by the use of interrupts. However, the new version of the device will include more sensors, such as i already explained, and here is where my problem began.
Buttons certainly don't need interrupts.
Sure, but what about when the arduino is sleeping? The board could be only waked up by inner or external interrupts. So, AFAIK, the button need an interrupt if you do not want to way up the board every few seconds to check it the button is high or low. I repeat that AFAIK... and i am a noob, so, if you know how to do it in other way, i am ready to learn!! So, culd you point to me toward where to read and learn about what you propose?
What is the alarm from the RTC?
Some RTC have the option to produce and square wave out, and few others trigger an interrupt when the time of the RTC reach a selected time, or date or every X minutes or seconds, or hours.... Then, if you program the RTC to produce an interrupt... you could send the board to sleep without take care about the time because the RTC will send the interrupt to a pin of the board what was configured to "listen" the RTC.
What sort of water flow meter have you? How many litres per minute? How accurate do you need to be?
If you want to use a button to wake up the Arduino then just dedicate one button for that.
If you want to use the RTC to make the Arduino every hour then the same pin that is used for the button wake-up could be used for that. I can't think of any other reason to use an RTC interrupt. Once the Arduino is awake it can read the time whenever it wishes.
I guess my question about the flow meter wasn's as clear as I thought. I was really trying to find out what is the least amount of effort the Arduino needs to expend without causing an unacceptable error in the total amount of water that flows.
For example, does the flow rate from your pump vary much? If not time will be a good indicator of volume once you have calibrated the flow rate.
Perhaps it would be sufficient to check the flow rate for 5 seconds every 10 minutes? Or maybe just check it once per day to verify the pump performance?
If you want to use a button to wake up the Arduino then just dedicate one button for that.
This is exactly what i have on mind
If you want to use the RTC to make the Arduino every hour then the same pin that is used for the button wake-up could be used for that. I can't think of any other reason to use an RTC interrupt. Once the Arduino is awake it can read the time whenever it wishes.
Exactly what i have on mind. Arduino is sleeping, at the selected time it awake the arduino what reads the time, reads the sensors, save the data, made decissions about if to start or not the irrigation, and then go to sleep again until the next time or an awake from the user
I guess my question about the flow meter wasn's as clear as I thought.
No, i am sure you were clear.... the problem is english is not my native language and sometimes my understanding is not complete. Now i see what you mean...
At this stage i don´t know how constant is the flow... But, such as a working hypothesis i will think that yes, it is a constant flow. Then, you are completely right, i don´t need to measure the flow continuosly... may be once per irrigation to ensure the flow is the same, and made the calculations based on time.... Just only one question,how could this affect to the interrupt pins? FOr this kind of cases, could i use a different procedure?
If you run the pump after everything else is done and plan the pump run based on time then after the pump is started you can run the flow calibration test. During the calibration test the Arduino can devote all its time to that task and just use a loop to poll the output from the flow sensor. The calibration test will conclude long before the pump is due to stop.
You can then update the pump run time based on the calibration test.