Team In Progress: Color Sensing Motor Control – Code

The code behind the project is fairly simple.

To establish communication between the Arduino and the color sensor, we sent register address and the byte value we want to write the magnetometer and loads the destination register with the value that was sent. To receive raw values, we send a register value address to a function and it will return the byte values for the magnetometer register’s content. Once the raw RGB values are read from their respective registers, they were converted into integers.


To determine the color being sensed, we compared the RGB values and found the color with the highest color value, then made that color the sensed color.


Once the color has been sensed, the Arduino sends out a character to the A3BU through UART.

The A3BU then takes the received character and adjusts the motor speed accordingly using Pulse Width Modulation.


Team In Progress: Color Sensing Motor Control – Schematics

Driving a DC Motor


Our team implemented a DC motor into our project using the circuit shown above. We used the SDA pin on the J1 header of the A3BU Xplained board to provide the signal, and we used 4 AA batteries as our power supply. The diode, wired in parallel with the motor, protects the transistor and the A3BU from possible reverse current. (Reverse current occurs when power is removed from the motor.) The diode does not affect the circuit when the power supply is on because, in this case, the diode is reverse-biased (meaning that it blocks current) and all the current flows through the motor. The transistor is used as an amplifier. The transistor has three pins (Collector, Emitter, and Base). The motor is connected to the Collector and the A3BU is connected to the Base. The Emitter is connected to ground. The A3BU cannot supply enough current for the motor to start turning, so the A3BU is connected to the transistor’s base, supplying the base current. The base current is multiplied by the gain of the transistor, and this current (the collector current) is supplied to the motor. The resistor is added to the circuit to create this base current. The high value (2.2 kΩ) of the resistor is used to limit the current going into the base since base currents are typically very small.

Team In Progress: Color Sensing Motor Control – Big Picture

The purpose of this project was to use the problem solving skills and the knowledge that we gained throughout the semester to create some type of interface with our A3BU. We chose to build a device that would turn a motor at different speeds depending on various colors that the device would be sensing.

The project was broken down into 3 different parts. The first part of the project was communicating with the color senor. This was accomplished by communicating via I2C (Inter-Integrated Circuit) to an Arduino Uno. I2C is a protocol intended to allow multiple slave digital integrated circuits to communicate with one or more master chips. Like SPI (Serial Peripheral Interface), it is only intended for short distance communications within a single device. However, it is also like Asynchronous Serial Interfaces (such as RS-232 or UARTs); it only requires two signal wires to exchange information. These two signals are SCL (the clock signal) and SDA (the data signal). SCL is a particular type of signal that oscillates between a high and low state and is used to coordinate the actions of a circuit. SDA is the physical transfer of data (a digital bit stream) from point-to-point. The second part of the project dealt with sending data from the Arduino to the A3BU. We chose to do this through a single line; TXD from the Arunino to RXD on the A3BU. However, in order to accomplish this we needed to create a voltage divider circuit to step down the 5V from the Arduino to 3.3V (the maximum voltage for the A3BU). The third part of this lab dealt with driving a DC motor. After the color sensor detects either a blue, red, yellow, or green card, the data is sent from the color sensor to the Arduino and then to the A3BU via TXD/RXD. We wanted to control the motor’s speed based on which color is detected. This was accomplished by using Pulse Width Modulation (PWM). In the code, when green is sensed by the color sensor, there is an 80% duty cycle of 3.3V applied to the motor circuit, which causes the motor to spin at a high speed. When yellow is sensed there is a 60% duty cycle, which causes the motor to spin at a lower speed. When red is detected, there is a 0% duty cycle, which stops the motor. Blue is used as an initialization.

At the end of the lab we were able to create a device that would turn a fan at different speeds based on what color is detected. By doing this we were able to gain a much better understanding of I2C, how to communicate amongst microcontrollers, and how to create and implement a circuit to drive a motor.


Demo Video: