Rodent Motion Tracker and Logger - How Does Light Effect Nocturnal Animals? - Using Raspberry Pi + Arduino
by ashers_workshop in Workshop > Science
42 Views, 1 Favorites, 0 Comments
Rodent Motion Tracker and Logger - How Does Light Effect Nocturnal Animals? - Using Raspberry Pi + Arduino



Hi, my name is Asher. I am a student in high-school and this project is part of my Biology fair.
Before I explain what exactly this is and why I did it, I need to say that this project mainly focuses on the circuitry and knowledge gained from this can be used in many more use cases.
At first, I had no idea what I would do. I knew for sure that I wanted to do something with living creatures, i.e. not something in a petri dish. For one, I have never played with that type of stuff and in my mind it was too complicated. As you will see, my project ended up being more complicated than I anticipated. My first idea was to do something with ants and scents. Unfortunately, it is winter currently and all the ants are way deep underground. I really didn't want to deal with ants in my house, so I had to think of something else... The following project is the work of my crazy brain that ended up doing something way overkill and time-consuming without much of an idea on what was going to happen.
Recently, I have been interested in sleep and circadian rhythms as well as things that effect it, one of those being light. The original idea was very simple, but soon escalated out of control. Mice, especially female mice, are known to eat less under stress. Mice, being a nocturnal animal, will get stress when under constant light. My plan was to simply have one cage with a very bright light 24/7 and another one with 4 hours of light per day. I was somewhat unsure of what my results would be before long, I decided that I would make a system that would track the motion and food eating times as well as a rough estimate of their food consumption. All the measured data would then be logged into a .CSV file for graphing.
This was definitely a crazy project. I ended up with 1 male mouse that made ~18 pups, the mothers killed 15 of them, and 3 adult mice that died!
I hope someone will find this useful and informative. At the end, I will give a brief explanation of my results. At the time of writing (2/5/25), the experiment is still in progress and I will update this Instructable when it is finished. I will also be making a couple posters.
If you have any questions, I am happy to answer. (if i can)
- Comment or message me here on Instructables
- DM on Instagram @ashers_workshop
- Email me at: chungcrafters@gmail.com
Supplies

















Gathering Supplies
ELECTRONICS:
- 4X 5VDC - $6.99
- 2x IR distance sensor - $8.79
- 2x PIR Motion sensor - $8.49
- 1x Arduino Uno - $14.99
- Raspberry Pi 4 - $61.89
- Regulated Power Supply - $25 (12v would be ideal, but I already had 24v PSU. You would only need 2 Buck converters if you use 12v LEDs and 12v 5a PSU)
- 4x Buck converters - $10
- LED Lights - $15.99
- 4x sets Barrel Plugs - $8
Cage Construction:
- 1/2" Plywood - $25
- 2x 2x4 $8
- <2kg Filament - $25
- Nails
- Black/White Paint - $50
Mice food/bedding
- Mice Feed - $10
- Mice Bedding- $10
- 2x Small sippers - $15
Female MICE - 6x - $2.49 (You cannot purchase live mice online. I got mine at Exotic Pets in Oklahoma City. I Bought feeder mice. Maybe not the greatest idea??)
Other Components:
- Display ~$60
- Keyboard ~$10
- Mouse ~$5 (lol, a computer mouse this time)
- Cables ~$20
- 6x2.5mm Neodymium Magnets
Total cost: ~$300-$400
Personally, I had quite a bit of the components already. Parts that I collected from old 3D printers or stuff I had from previous projects, so the total cost for me was about ~$160
Also, an older Pi will work, but I just ended up having a Pi 4.
Tools needed:
- 3D Printer
- Circular saw
- Basic small electronic tools
- Jigsaw
Other basic around the house tools.
Building the Cage - CAD



Cage requirements:
- Needs to be large enough for at least 3 mice and not be overcrowded
- Walls need to be high enough that they cannot jump out
- Needs to be light sealed
- Have adequate ventilation
- Smaller hatch as a cover
- Able to feed mice without interfering/interacting with mice. Feed from outside without letting mice escape.
- Proper water sipper
With those requirements, I came up with this:
FINAL CAGE BOM and Dimensions:
- 16” Wide 24” Long 16” High
- 1/2th inch plywood
- 2x 2x4
- Black paint for Control Group
- White paint for the experimental Group
I have the .pdf drawings for the cage as well as .f3d and .stp files so you can look at the assembly on your machine.
P.S. Excuse my drawing skills. The drawings are pretty messy
I also didn't end up doing a tapered top, instead I went for a flat one as it is much simpler and uses less wood.
Constructing the Cage















Follow the CAD assembly or the PDF plans. This part is pretty straightforward if you have the necessary tools and experience. Please do not hurt yourself.
I split the 2x4 into 3pcs with the table saw. It would have been easier if you just us 1x1, but this is what I had.
Using a brad nailer to speed up the process, I nailed it together.
Feeder + 3D Printed Parts















The design of the feeder allows me to access the feeder without putting my hand in the cage and interfering with the mice or getting my hands dirty. (mice poop everywhere) In addition, with the magnet pressed into the printed parts with reverse polarity makes the door close when I pull out the dish to prevent mice from escaping. It also houses the distance sensor, which counts how much the mouse is eating. For the door, I used 1.2mm music wire, but a paper clip should suffice. Simply push the wire in and bend the end of it to prevent it from sliding out.
I raised the feeder so that the mice do not trip the sensor when they are not eating. From my observations, the mice do not typically sit at the food dish and do nothing. It is a rather tight platform, so they are only on it while eating.
If you do not have any extra sensors, do not install them now. You will see why in the next step.
Assembling Electronics







I used 8"x10" 1/4" Lauan as the base for all the electronics. On the back I attached the 24v PSU with double-sided tape.
Using small drops of hot glue, I attached the rest of the components.
- 4x Buck converters
- 4x Relays
- Breadboard
- Arduino Uno
- Raspberry Pi
Connect the power strip on the left side of the breadboard to the 5v from one of the buck converters. This is for the PIR and IR distance sensors. Connect the right power strip to 2 of the buck converters at 12v. This is for the LED lights.
Connect all the components to the breadboard and follow the Arduino Pinout to connect everything properly. Click Here to see my Google Sheets for the pinout, or look at the screenshot.
Congratulations! You are almost done with all the hardware stuff. We still have a lot to do, so before you take your circuit off the workbench, go ahead and connect 1 LED to the relay, 1 IR distance sensor, and 1 PIR sensor to the appropriate peripherals.
The Nightmare - Making Everything Work Together

This was definitely the most dreaded part for me, mainly because I can't code. Every time I do a project that requires interaction with a computer, whether it be a 3D printer, Laser cutter, etc.. making the code or firmware work is always a nightmare. At least for me. With this step, I consulted the help of my friend/enemy, ChatGPT. After arguing with the 4o model for hours and only getting super buggy code or other nonsense, I had no idea what to do. Finally, I used the o1 model, it finally worked. (I got ChatGPT Plus just for this project. -$20)
This was my first prompt:
"Here are the exact details to remember for the arduino part. I may change and tell you other details in the future. If anything is unclear make sure you ask me and PLEASE DO NOT assume something. Ask me questions on stuff you do not understand completely. All I want is for the arduino uno to get live data and send it to the raspberry pi. I will have 2 sensors in each group. Group 1 is Control Group, Group 2 is the experimental group. Both groups have the same sensors. SENSORS: 1 in each group HC-SR501 PIR Infrared Sensor This will be used to track the motion. IR Infrared Obstacle Avoidance Sensor Module for Arduino Smart Car Robot 3-Wire Reflective Photoelectric for Arduino Smart Car Robot This sensor will be used at the food dish. When it finds a distance less than 3cm than it counts that the mouse is eating. The main goal for this project is to see how mouse react to light. The control group will have a dim light for 4 hours and a bright light for 4 hrs. The rest of the day will be in complete darkness. The experimental group will have really bright LED lights nonstop for a whole week. What I need the arduino to do is to get the data from the 4 sensors and send the data it gathers to the pi over USB serial."
However, the final code is significantly different. Here is what it does:
- Uses the tkinter GUI
- UI lets you the schedule for the Lights
- UI lets you test the Lights
- UI shows a live counter of the movements and food that resets every hour
- Program counts all the movements and instances that the mice go to the food dish
- Program logs the count of the movements/feed at the hour into a .csv file. Example, Pi turns on at 8:30 next log happens at 9:00 not 9:30. I did this to make sure that the timing in the graph wouldn't be all over the place in times.
- Terminal window shows the live data coming in as well.
Things to adjust on the sensors:
PIR Motion sensor - Make sure that the jumper is set to repeat trigger instead of single trigger. I left the motion sensitivity to default and it worked fine.
IR Distance Sensor - Adjust the screw so that it trips around 3cm. There is a little indicator light on the IR distance sensor that shows when it is tripped.
Flashing Arduino Code
Using Arduino IDE either on the Pi or your PC flash the following code to your arduino.
Creating Virtual Environment + Running the Code


Now that 1 of each sensor is temporarily connected, let's go through all the necessary steps to run the code and make sure that the Arduino and Pi are communicating properly.
1. Update and Install System Packages on Raspberry Pi
Open a terminal on the Pi (or SSH into it) and run:
Then install Python and other tools:
- python3-venv: lets us create a virtual environment.
- python3-tk: ensures tkinter (the GUI toolkit) is installed system-wide.
2. Verify the Arduino Is Recognized
- Plug the Arduino into the Raspberry Pi’s USB port (if not already).
- Run:
If you see something like /dev/ttyACM0, the Pi recognizes your Arduino.
If you see no output, try replugging, or check for /dev/ttyUSB*
If you see something like ACM1, then change it to ACM1 in the Python code. By default, it is ACM0. The number changes depending on what USB port you have it plugged into.
3. Create a Project Folder and Virtual Environment
- Move into your Documents folder (or wherever you’d like your project):
- Create and enter a folder for your project, e.g. mice_tracker:
- Create a virtual environment named venv:
- Activate the virtual environment:
- Your terminal prompt should now begin with (venv).
4. Install Python Packages in the Virtual Environment
- Upgrade pip:
- Install pyserial (and any other libraries you need):
(tkinter is already installed system-wide from Step 3, so no need to install inside venv.)
5. Run the Pi Code
- Place the raspberry_pi_code.py file into the mice_tracker folder
- Make sure you’re still in the mice_tracker folder with the virtual environment active (prompt says (venv)).
- Run your script:
- A tkinter GUI window should appear (the sensor counts, schedule fields, etc.).
- If the Arduino is sending data, you’ll see lines in the terminal like:
- The code will log data to mouse_activity_log.csv on the hour (and whenever you click “Log CSV Now”).
6. Confirm Communication and Logging
- Arduino TX light blinking means the Arduino is sending serial data.
- Terminal “Received” lines mean the Pi is reading data.
- Light On/Off Buttons in the GUI should make the Arduino’s RX light blink, turning lights on/off.
- CSV File (mouse_activity_log.csv) should have new entries each hour (or when you manually log).
- If you want to watch the CSV in real time, open a separate terminal window and run:
- You’ll see each new row appear as it’s logged.
Testing:
Hopefully you made it through this step with minimal trouble, unlike me who spent over 12 hours trying to figure this out.
Go ahead and press the Test light buttons to make sure your relays are working properly, and move your hand across the sensors to see that they are sensing correctly. Press the log .csv now button, then open the .csv file.
You should see something like this
This includes that date, time, the count of the sensors, and the lighting schedule at the time of logging.
Downloads
Final Setup





Hot glue in the PIR Motion sensor and install the LED bars on the top part of cap. Use extra wiring or jumper cables from the PIR and IR sensor and connect them to the breadboard. Plug the LEDs into the relays using barrel plugs, or directly connect them to save a little $$.
Power everything on and run the code!
Here is how to run the program after the initial setup is done:
- Move into the mice_tracker directory
- Activate the virtual environment
- Run the program
Hopefully everything is working smoothly for you by now! If not, Google.com is your friend, LLMs like ChatGPT are also quite helpful, or just reach out to a friend, or ME!
Congratulations
Now that you wasted a bunch of time and money on this project, you can track the activity of your rodents for whatever crazy reason you want!
Different possible cost saving measures:
This project can be quite expensive. I definitely could have made this project cheaper and simpler. Below is some of my reasons to take the path I took, and ways it could be done differently.
- Only have 1 cage, test different light schedules on different dates
- Skip the Pi altogether. Have the Arduino log directly into SD Card
- Use small LCD display module with buttons on arduino to save on an actual monitor
- Use of a small wall brick PSU
I have multiple Pi's already, and I really like being able to have the GUI being able to easily schedule the lights as well as see the live data. In addition, this was a good excuse for me to learn more about Python and C++.
The rest of this Instructable will be my personal observations. I believe I got some pretty interesting results, as well as some crazy things that happened. Please note that my results are likely to be skewed because of the many things that happened.
My Observations - How Does Light Affect Mice Activity?










A lot of crazy things happened in this experiment and at the time of writing I am still not done collecting data.
I already have some basic data worth sharing.
As I mentioned above, you probably don't need to have two groups, instead you could just test them at different times. Due to pregnant mice, death, and a male, the experimental group data is pretty erratic.
After having the EXP GRP under bright 24/7 Light for several days, I switched it up and gave the CTRL GRP 24/7 Light. The conclusions of this experiment at this point will mainly come from data gathered in the Control Group cage to mitigate the mostly unusable data gathered from the Experimental Group Cage.
Unexpected happenings:
I specifically asked for 9 female mice because:
- Less aggressive than males
- More likely to express stress in ways I could measure
However, somehow when I bought them, the handler got them mixed up. I ended up with 1 male mouse that ended up in the experimental group cage. The male mouse impregnated the 2 other mice.
I purchased an extra mouse, which is the reason why I had 9. I ended up receiving 3 of the following mice colors, 3x black, 3x white, and 3x brown. The black and white mice were very similar in size and the way they socialized/acted, while the brown mice were much smaller, were less active, and more to themselves. Before I was even able to complete the circuit to track them, all 3 mice had died. When I found the male mouse, I fed it to the cat. Since the mothers were quite stressed from the light and noise, they killed the babies within a couple of days. After all that I have 2 mice in each group and am getting clean data. Whew! What a relief. Below are my current observations as of February 10, 2025. I will end up writing a final paper, making a presentation, and making a poster about my project. That will be finished in March. I plan to update this Instructable with additional information gathered and more detailed observations. This Instructable is in the Circuits category and not Biology. I hope you found this Instructable informative, as I gain more data on the mice I will update this accordingly, but as the main point was to show how to make a tracking system that logs into a csv file for graphing, I think I gave decent coverage.
I ended up with the following light schedules:
EXP GRP - Light 24hrs/day
CTRL GRP - Light 6hrs/day from 6-12
Due to the lack of time/resources, I was unable to test with a dim light, which would have been ideal for the mice because it would give them the most realistic conditions.
Observations Group With Normal Amounts of Light
- Predictable activity/eating times
- Spike in activity/eating when light turns on
- Significantly decreased activity/eating after 1hr of light
- Significant increase in activity 12 hrs after light turns off
- Increased food consumption for 6 hrs after the light turns off
Observations Group With Constant Light
- Less predictable active/eating times
- Activity and eating times do not perfectly correlate
- Activity mainly affected by external factors. (Household noise)
Observations from the Control Group Cage
- Noticeable decrease in activity and food consumption when under constant light
- Unpredictable eating times
Download the chart.svg to see a detailed view of the graphed data.
Here is the link to my Google Sheets that has all my observed data - Biology Fair Spreadsheet
Summary of my observations
In this experiment, I observed how different light conditions affected mice activity and food consumption. Although there were several challenges—such as unexpected pregnancies, mouse deaths, and erratic data from the experimental group—the control group provided valuable insights. When exposed to constant 24/7 light, the mice displayed less predictable activity and eating patterns, with behavior largely influenced by external factors like household noise. In contrast, the group with a light schedule of six hours per day, exhibited more regular activity and feeding patterns. There was a spike in activity and eating when the lights turned on, followed by a decrease after an hour. Activity significantly increased for the following 12 hours after the lights turned off, along with elevated food consumption during the following six hours. Notably, when the control group was placed under constant light, their activity and food intake became unpredictable and declined overall.