Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
TCS3200 Color SensorIntroduction | ||||||||
Line: 6 to 6 | ||||||||
The TCS3200 color sensor comes on PCBs with slightly different layout:
| ||||||||
Changed: | ||||||||
< < | The main difference between the modules is that one of them pulls out the TCS3200 OE (Output Enable) line, while the other does not. | |||||||
> > | The main difference between these modules is that one of them pulls out the TCS3200 OE (Output Enable) line, while the other one does not. | |||||||
Changed: | ||||||||
< < | The TCS3200 uses an array of 64 photo cells, where 16 of the cells are equipped with a red, green or blue filter while the remaining 16 cells have no filter at all. | |||||||
> > | The TCS3200 uses an array of 64 photo cells, where 16 of the cells are equipped with a red, green or blue filter respectively, while the remaining 16 cells have no filter at all. | |||||||
![]() | ||||||||
Line: 34 to 34 | ||||||||
The LED control line allows switching the illumination LEDs on and off.
Understanding the module and developing a driver for it | ||||||||
Changed: | ||||||||
< < | The software is available on github: | |||||||
> > | The software is available on github: https://github.com/uraich/TCS3200-MicroPython![]() | |||||||
First steps | ||||||||
Changed: | ||||||||
< < | A driver in MicroPython is usually implemented as a Python class. The object creation method init initializes the chip. In case of the TCS3200, the GPIO lines used to control | |||||||
> > | A driver in MicroPython is usually implemented as a Python class. The object creation method __init__ initializes the chip. In case of the TCS3200, the GPIO lines used to control | |||||||
| ||||||||
Changed: | ||||||||
< < | To have a first preliminary check, we can implement, kin addition to the init method, a method that switches the LEDs on and off. I prefer to also have a debug method allowing me to switch debugging messages on and off. | |||||||
> > | To have a first preliminary check, we can implement, kin addition to the __init__ method, a method that switches the LEDs on and off. I prefer to also have a debug method allowing me to switch debugging messages on and off. | |||||||
Changed: | ||||||||
< < | This is implemented in the led.py program. To ease development, the driver and the application using it, are combined in the same file. Once the driver is sufficiently tested, these two parts will be separated in two distinct files. The driver will be uploaded to the /lib folder on the ESP32 to make it permanently accessible. | |||||||
> > | This is implemented in the led.py program. Another short MicroPython program: switchOffLeds.py is provided to switch the LEDs off. This can be run on the ESP32 from the command line using the shell script switchOffLeds. Make sure that execute permission is given for the script.
To ease development, the driver and the application using it, are combined in the same file. Once the driver is sufficiently tested, these two parts will be separated in two distinct files. The driver will be uploaded to the /lib folder on the ESP32 to make it permanently accessible. | |||||||
In Python there are no public and private variables. There is however a convention that variables whose names start with the '_' character to be considered private. | ||||||||
Line: 55 to 57 | ||||||||
Towards reading the frequency of the OUT signal | ||||||||
Changed: | ||||||||
< < | In order to better understand the TCS3200 it is necessary to get a feeling for the frequencies it emits. In order to measure this frequency, we must be able to set the filter and the frequency divider. This is done in the program filter_and_freq.py. The test program sets debugging mode, and it writes and reads back the filter and frequency divider settings. | |||||||
> > | In order to better understand the TCS3200 it is necessary to get a feeling for the frequencies it emits. In order to measure this frequency, we must be able to set the filter and the frequency divider. This is done in the program filter_and_freq.py. The test program sets debugging mode, and it writes and reads back the filter and frequency divider settings. | |||||||
Measuring the frequencyIn order to measure the frequency, the time elapsed for the detection of a number of OUT signal cycles is measured. The number of cycles to be used is set in the _cycles variable. Of course, we again need getter and setter methods to control _cycles. We attach an interrupt handler to the OUT pin (_cbf for callback function). The number of cycles, already measured, is saved in the _cycle (without the "s"). This value is set to zero when the interrupt handler is started (connected to _cbf). When the first rising edge of the signal is seen, the current system clock in us is saved in _start_tick. For each rising edge of the OUT signal, _cycle is incremented until the value In _cycles is reached, in which case the system clock is saved in _end_tick. The duration between the start of the measurement and the moment the number of requested cycles has been seen is then | ||||||||
Line: 64 to 66 | ||||||||
and the frequency in Hz is 1000000 * _cycles / duration. The factor 1000000 comes from the fact that the duration is measured in us while the frequency is calculated in Hz. | ||||||||
Added: | ||||||||
> > | This program is implemented in meas_freq.py.
It shows that for clear filters, the frequency divider set to 2% and a white target, I measure a frequency of 2.349 kHz. When using a black target, the frequency drops to 380 Hz.
This teaches us an important lesson: The time between two rising edges for the white target is just 435 us. If we change the frequency divider to 20% this time would be reduced to 43 us, which is simply too short an interval for our interrupt handler. The program can handle a 20% frequency divider setting for the black target, in which case the interval is 263 us (1000000/3800), but it will crash for the white target because it will receive new interrupts while the old ones have not been entirely treated. If we want to read the OUT signal at full speed, then an external high speed counter is needed. Otherwise, we can fix the S0 and S1 signals to 0 and 1 respectively and therefore fix the frequency divider to 2%. This liberates 2 GPIO lines for other purposes. | |||||||
-- ![]() Comments | ||||||||
Line: 76 to 88 | ||||||||
| ||||||||
Added: | ||||||||
> > |
|
Line: 1 to 1 | |||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Added: | |||||||||||||||||||||||||||||||||||||||||||||||||
> > |
TCS3200 Color SensorIntroductionThe TCS3200 color sensor comes on PCBs with slightly different layout:
The TCS3200 uses an array of 64 photo cells, where 16 of the cells are equipped with a red, green or blue filter while the remaining 16 cells have no filter at all.
The control signals S2 and S3 allow selecting cells with a particular filter:
The output frequency is passed through a frequency divider which scales down the frequency output on the OUT line as follows
The LED control line allows switching the illumination LEDs on and off. Understanding the module and developing a driver for itThe software is available on github: First stepsA driver in MicroPython is usually implemented as a Python class. The object creation method init initializes the chip. In case of the TCS3200, the GPIO lines used to control
To have a first preliminary check, we can implement, kin addition to the init method, a method that switches the LEDs on and off. I prefer to also have a debug method allowing me to switch debugging messages on and off. This is implemented in the led.py program. To ease development, the driver and the application using it, are combined in the same file. Once the driver is sufficiently tested, these two parts will be separated in two distinct files. The driver will be uploaded to the /lib folder on the ESP32 to make it permanently accessible. In Python there are no public and private variables. There is however a convention that variables whose names start with the '_' character to be considered private. Very often, classes have variables that are accessed through getter and setter functions. These can be implemented with the decorators:
Towards reading the frequency of the OUT signalIn order to better understand the TCS3200 it is necessary to get a feeling for the frequencies it emits. In order to measure this frequency, we must be able to set the filter and the frequency divider. This is done in the program filter_and_freq.py. The test program sets debugging mode, and it writes and reads back the filter and frequency divider settings. Measuring the frequencyIn order to measure the frequency, the time elapsed for the detection of a number of OUT signal cycles is measured. The number of cycles to be used is set in the _cycles variable. Of course, we again need getter and setter methods to control _cycles. We attach an interrupt handler to the OUT pin (_cbf for callback function). The number of cycles, already measured, is saved in the _cycle (without the "s"). This value is set to zero when the interrupt handler is started (connected to _cbf). When the first rising edge of the signal is seen, the current system clock in us is saved in _start_tick. For each rising edge of the OUT signal, _cycle is incremented until the value In _cycles is reached, in which case the system clock is saved in _end_tick. The duration between the start of the measurement and the moment the number of requested cycles has been seen is then duration =_end_tick - _start_tick and the frequency in Hz is 1000000 * _cycles / duration. The factor 1000000 comes from the fact that the duration is measured in us while the frequency is calculated in Hz. Comments
|