Go Down

Topic: Counting Ants (Read 4057 times) previous topic - next topic


Goal: To make Arduino-based ant counters that can eventually log data for months in ant hills. This information should help figure out how the behavior of colonies of these social insects change as they age. Queen harvester ants may live for a decade and colony behavior evolves over this time.  When she dies the colony dies. In addition to recording temperature, humidity, and light level, we have to quantify ant activity. We'll test it first using ants living in a lab before trying these loggers in an Arizona field this summer.

I'm very new to programming and am starting with an Arduino Uno with a Logger Shield w/ RTC and SD card mounted. I have successfully worked through the well-written introductory guidance and have logged one second light and temperature data from a refrigerator, and graphed files from the SD card in Excel. They work great! I then changed the delay between entries and sync intervals from 1000 to 60000 mills and, sure enough, the Arduino logs well at that rate. I can follow the well annotated code - but don't understand many of the twists and turns it makes and haven't a clue how to write code to add features without ruining what works.

To minimize noise in the data, I would like to make registers, zero them, and add 1/60 each of 60 readings taken each second to the respective registers before writing this averaged one minute data to the SD Card. How would one do this?

The next step: two Infrared (IR) sensors look at an IR LED and when an ant blocks the radiation, from first one, then both, then the other, I'd like to increment an "out" register, and if in the opposite sequence, an "in" register (with carry over if an ant happens to be in the beam when writing one minute data to the SD card). To save battery power, the IR LED should be turned on only 1ms every 100 ms (a guess - shorter duty cycle if possible) and the output of the two IR sensors only read then. I'm sure the Arduino can do all these things - but it will take someone that knows more than I to write the instructions for it to follow.


ir beam ants ? i dont think it will work out ! what is the size of the ants you are working on ? to block a n ir beam youll need ants of the sizze of your fingers !


Harvester ants are large (for ants: up to 5 mm) but not as large as a finger. I can use a pinhole to limit the size of the beam so that it only "illuminates" the IR LEDs that are 4 mm apart. The "tunnel" that the ants travel through, and the sensors are in, is 5 mm wide and the sensitive areas of sensors are maybe 9 mm apart.

Big Oil

It would be good to post the code that you have now using the # button. 
Since you have several things going on you may have to avoid using delay(), and instead use interrupts, or look at the digital--> blink without delay example in your software.  Delay basically freezes the entire program for that time.
To average using delays it would look like:
data1= analogRead(sensorpin);
delay(17); // 17 milliseconds
data2= analogRead(sensorpin);
delay(17); // 17 milliseconds
data3= analogRead(sensorpin);  //and so on

average= (data1 + data2 + data3) / 3 ;

You may not want to average an entire second if the ants are moving fast. 


I'm only just beginning to learn how to develop code for this so I am now using the code that comes with the Logger Shield: https://github.com/adafruit/Light-and-Temp-logger

I see how you averaged data below but wouldn't that take 60 bytes for each of the five data sets? Then having to retrieve them all before adding them together before averaging them. Wouldn't it be easier to use only one variable per sensor, clearing it at the start, then adding 1/60 of each number for 60 times then simply printing the number? That's a different way to make an average that can be done in the same line of code that converts the voltage to degrees C, for example. float temperatureC = float temperatureC + (voltage - 0.5) * 100 / 60  (from a line in the above code) but I don't know how to do this 60 times, or otherwise keep track of when to log the numbers, when the minute is up. How does one use interrupts? What's the digital--> blink you referred to?

I also don't understand the statement: "You may not want to average an entire second if the ants are moving fast." What I hoped to do was to simply count ants going from left to right, and from right to left (through a tunnel) and do this for a minute, along with the temp., humidity,and light readings, then print all five to the SD card. But I don't know how to look at a series of sensor readings, as an ant interrupts the beam to first one sensor, then both, then the second and turn this sequence into a "one ant going out" count. The tunnel will be narrow enough so that only one ant at a time can go through (hopefully).

Big Oil

Feb 02, 2011, 10:35 pm Last Edit: Feb 02, 2011, 10:37 pm by Big Oil Reason: 1
You can copy and paste the code using the # button.  
If you use delay(17);  then you won't be able to take any other readings during that time, so instead you could look at examples-->digital-->blink without delay. (it's in your arduino IDE program)
I was thinking that if the ants move really fast then you might only get two positive readings out of 60, which will average out to a low number.  There's going to be a little jumpy noise in your readings anyway, your null reading might be between 165-175 so a small reading might average into that and would look like noise.  
But I don't know how to look at a series of sensor readings

You can use analogRead for each of the pins and read one after another
Looking at a different temperature logging project might help with coding ideas.


what is the speed at which the ants would be moving generally a normal analogread would be fast enough to read the ir beams and what kind of an ir sensor are you using a custom one i guess generally ir sensors are analog (varying voltage ) http://www.arduino.cc/en/Reference/AnalogRead look there and you will know hoe to get the sensor's readings something meaningful to you


I'm a nature photographer, I spend a lot of time watching ants and frankly I think you're trying to push sh1t uphill with a pointed stick. This is the old count the number of people in a room problem but a 100x worse because ants are totally random in their behaviour.

Assuming that the tunnel has to be large enough for two ants to pass each other there will be all sorts of scenarios where two are read as one, one stays still in front of the beam while 10 walk past, another gets half way through the beam then decides to go back out, an ant walk in carrying a seed but the gap between them is large enough to register as a break, a worker ant meets a soldier directly in front of the beams, they sense each others pheramones then both the worker and the soldier go out, all combinations of the above at the same time,  etc etc.

And if you can't limit the tunnel to only two ants all the above x 10.

There has to be another way, surely you don't have to count every single ant, maybe some system that worked on "ant density" using weight (how much does an ant weigh?), average light over the whole tunnel or something.


Rob Gray aka the GRAYnomad www.robgray.com


Thank you for your comments. It'll be only through iterations that we'll get equipment that works - even though it may evolve into what is possible, not what we dreamed.

The sensor tunnel will essentially block the entrance of an ant hill and be narrow enough for only one ant to go through at a time - that happens in their tunnels if stones prevent their moving them. If that bothers the ants too much (so that they make a way around the sensors) we'll widen it so that two can pass through at the same time. Initially, the only information we wanted was "When do the ants from different age colonies start foraging in the morning, and how late to they stop?" The premise: middle age colonies are more aggressive than younger ones to make sure they get all the worms, if they can (like early birds). So it doesn't really matter if we miss counting some, if we can simply record ant activity. Counting all day should be a function of colony size - without having to dig up the whole nest and count every ant -that does in the colony (which is the only way to do it now).


but I don't know how to do this 60 times, or otherwise keep track of when to log the numbers, when the minute is up. How does one use interrupts? What's the digital--> blink you referred to?

Each time you add to the average, increment a separate counter variable as well.  When the counter = 60, log and reset.  Something like:

Code: [Select]

  temperature += (voltage - 0.5) * 100 / 60;
  tempCounter++; //this can be defined as a byte (but if you want to increment up to values above 255, you'll need to use a larger variable
  if(tempCounter == 60){
    //write out your temperature
    //reset variables
    tempCounter = 0;
    temperature = 0;

There are several timer libraries available that will help you easily run the above code at some specified interval.  No need to reinvent the wheel, except as a learning experience.  But for that you can just read the code those libraries use.


Very good advice, Arduino Rocks - I'll try it :).

What I'm trying to prove at the moment is a circuit similar to the Honeybee Hive Traffic Counter:  http://www.discovercircuits.com/PDF-FILES/beectr.pdf  that uses the Arduino instead of flip-flops and counting module. Once I have the Arduino record a series of signals that represent an ant going first in one direction, then the other, I'll post them for suggestions on how to reduce the swings in analog voltages of the two side-by-side sensors viewing an IR transmitter to a single "in" or "out" count. If we don't succeed, we can always just record "an ant went by", like the bee counter. It would be nice to make an Arduino smarter than that.


Just wondering, how will you know if the ants build another exit from their colony? How will you get your mechanical tunnel into their dirt tunnel?

Biggest question, how do you know that adding equipment to their colony won't change the behavior you are trying to monitor?



The Arduino-based counters are supplements to weeks of actual daybreak to sunset personal observations, which has been the primary mode of taking data  at the site for many decades. But folks need R&R and you can't carefully watch more than a few colonies at a time. By watching and taking notes with a few instrumented colonies and a few pristine, of matched age, one can get an idea of the effect of partially blocking an entrance. Larger, and older, colonies have multiple entrances/exits so that they can be monitored before placing a sensor stick to see if traffic changes (the sensors are buried in a broken branch that looks natural so that passersby won't notice them and want to pick one up to examine it when the scientists are not there - the Arduino and battery will be buried a foot or two away and the cable covered - we chose IR sensors so that they don't attract attention at night).


My personal opinion is that you want to force the ants through a dark passage and use near infrared imagery to pick them up, then use machine vision techniques to track individual ants and track how many head which way.  This would be far beyond the ability of Arduino, but well within what you could do with gumstix or something akin to that -- an ARM and more than a MB of memory.


Just wondering, how will you know if the ants build another exit from their colony? How will you get your mechanical tunnel into their dirt tunnel?

Biggest question, how do you know that adding equipment to their colony won't change the behavior you are trying to monitor?

But that is already establish scientific fact: Measurement changes behavior.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131