You have a few options:
Note: The links given are just the first ones that come up. Most of them are pricey compared to other sites. eBay buy it nows can be fairly cheap for most of these items. Check other places, too, including Sparkfun.
- Ultrasonic Ping Sensor. This isn't ideal by any means, but they're easy to use. Just add one with an enclosure. It isn't ideal because it won't be accurate enough to tell when exactly it's empty. It might be a little over/under sensitive. Weight would be harder, though if it shifts at all.
- Color sensor (preferable, but costs more). You mentioned this and this is probably the best idea. It'll give you analogue values (to read with Arduino) for red, green, and blue. [Assuming that the higher the content of that color, the higher the voltage,] white should be pretty much all of those at 100% and brown I would image that most of them would be about 30-40%. (More later, if you don't understand this, skip this part)
I'm going to go into more detail on the color sensor:
Colors are read with three values here: red, green, and blue. There are three sensors on the board. One filters out everything except red, another blue, and the other green. The sensors on the board detect the amount of incoming light and then convert that to a voltage: between 5V ("100% red/green/blue") or 0V ("0% red/green/blue"). This then can be converted into a number by the Arduino (0-1024 on an analogue pin).
A problem with lighting:
This sensor, from what I can tell, has a LED that's always on. You'll want to either desolder that or cover it up, unless you want an illuminated toilet paper holder. Now wait... your sensor will think that when it's dark, it's brown. No problem... here's something for you mind to think about.
Use a color picker like I'm using and select a brown. On the left of this one, you can control the "simulated brightness." Besides that is the hexadecimal form of RGB. You'll want to convert that to standard values with this calcuator.
In brown, the color blue is almost nonexistent. We should ignore that color, unless you want a way to double check that it's brown. I'd say it should always be under 40% for pretty much any shade of brown.
From what I can tell with a sample shade of brown, the red is pretty much 2x the green. That ratio may vary, but you should verify that it is somewhat existent. For white/grey/black, all the colors are proportional 1:1:1. You can use this to detect if it's dark, or if it's brown.
int rPin = A0; //Red Pin
int gPin = A1; //Green Pin
int bPin = A2; //Blue Pin
int ledPin = 13; //I made this code with a LED, not a speaker
boolean testTwice = false;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
int rValue = analogRead(rPin);
int gValue = analogRead(gPin);
int bValue = analogRead(bPin);
if (bPin < 256) { //If this is false, either something is in the way or it's white. Going to verify that something's not in the way...
int minRed = rPin * 0.4; //A little under a half for padding... this values may need to be adjusted. Adjust this one and the one below farther apart for less sensitive.
int maxRed = rPin * 0.6; //A little over a half for padding... this values may need to be adjusted.
if (gPin < maxRed && gPin > minRed) { //Verifys that red between minRed and maxRed
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
}
delay(5000); // No need to waste power... you might want to add an actutal sleep function if on battery power
}
First, it verifies that the blue is less than 25%. Then, it verifies that the red and green are equal... give or take 20%. (i.e. red = 100 green = 80, red = 110 green = 90, etc.). If that is true, you can pretty much establish that it's brown. Then, a LED turn on for five seconds.
You can modify the LED to be a buzzer if you want. You might have to adjust the if statement with verifying that the blue is less than 256/1024 (1024 = resolution of analog pin) if it isn't working in a lot of light or is working in dark light. Since the code later verifies that the red is about 2x the green, any shade of white shouldn't pass that if statement. You may also have to adjust the minRed
and maxRed
to be farther (if not detecting brown) or closer (if detecting brown when it's white). I can't really test this code with no color sensor.
Note: these can be a little fidgety. I would recommend a LED instead so the whole house isn't woken up in the middle of the night just because of low toilet paper. Also, if you obstruct the sensor at all... If it wasn't in the bathroom, I would recommend a camera and have it take a shot every time it gets very low with a reed switch like @jfpolipret's answer... but... (no pun intended) :)