Arduino Color Sensors - TCS230 & ISL29125

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop will be working with color sensors I'll show you how to calibrate and use two popular color sensors with the Arduino we're showing our two colors today so welcome to the workshop [Music] hello and welcome to the workshop into a very colorful episode of the drone ba workshop now we worked with color before in producing colors we've had RGB LEDs and displays that can produce a variety of colors but today we're going to be working with the opposite thing and that would be sensing colors now you could sense colors a number of different ways a very simplistic way of doing it would be to use a light sensor in place a color filter in front of it so that only one color of light would be able to activate the sensor a very sophisticated method a sensing color would be to use a video camera where we're going for the middle ground today we're going to look at a couple of dedicated color sensors and how we can use them with the Arduino so before we do that let's go and take a look at how color sensing works so let's take a quick look color sensors color sensors are used to distinguish color mixtures of red blue and green they're used in industrial process and quality control they're also useful in specialized lighting and photography applications the outputs can be digital analog or variable frequency color sensors consist of arrays of photodiodes there is a separate array for each of the primary colors these arrays are addressed individually the photodiodes used in color sensors has sensitivity that changes with color therefore the output for each color is not linear color sensors do not provide an RGB value as an output the sensors will require calibration in order to get an RGB value some color sensors can also sense infrared light now the first color sensor that we're going to look at is a very inexpensive and commonly available module and it actually goes by a couple of part numbers the most common is TCS two-30 but it's also called the TCS three to zero zero and they're basically the same thing now this is an older style of color sensor and it's not the most accurate one but if you calibrate it it can still be useful so let's go and take a look at that sensor right now let's examine the TCS two-30 also called the TCS 3200 this is a color sensor array that has a variable frequency output it operates with a voltage of two point seven two five point five volts and provides a TTL output the output frequency changes with the light intensity it consists of a raise of selectable color photo transistors there are sixteen transistors in each of the colored arrays the sensor allows you to select the output frequency scale to match with your microcontroller or other output device the module also has four white LEDs to illuminate the subject being sensed the TCS two-30 is very inexpensive and quite commonly available the sensor is available with or without a lens or light shield the s0 and s1 inputs on the sensor determined the frequency scaling the s2 and s3 inputs determined the photodiode selection here are the pin outs of the TCS two-30 on the left side we have the s0 input and the s-1 input these are the ones that determine frequency scaling the output enable which is generally not connected in most applications and the ground on the right side we have the s3 input and the s2 input these are the ones that determine what color is being sensed we have the TTL output this is the variable frequency output whose frequency varies with the intensity of the colored light and we also have the VCC or power connection so here's the TCS two-30 or TCS three two zero zero sensor and as you can see this is a very simple module it's got the sensor itself in the center and you'll notice that it's got a clear top on it and you can even look at it and see the actual color sensor and if you look close enough and it's surrounded by four white LEDs and these are illuminated to light up the surface that you're actually sensing now in each edge of the sensor we've got a series of connectors and these of course are the connectors that you're going to be using to connect it to your microcontroller and they come out to DuPont pins that are on the back over here so really not much more to say about this it's a very simple device in terms of hooking it up and which is exactly what we're going to do right now for this experiment you'll require an Arduino Uno and a TCS two-30 or TCS three two zero zero color sensor we'll begin by connecting the five volt output of the Arduino to the VCC pin on the color sensor we'll connect the Arduino --zz ground to the color sensor ground pin four of the Arduino will be connected to the s0 input on the sensor pin five will go to the s-1 input pin six will be connected to pin s2 and pin seven will be connected to the s3 input finally pin eight will be connected to the color sensors output and this completes the wiring now let's go and take a look at some of the code we'll need to use two the sensor so this is a sketch that we're going to use to calibrate our color sensor we need to calibrate the sensor first before we can use it and get correct RGB values out of it so we start off by defining the pins we've connected the sensor with so the s 0 through s 3 or connected depends 4 5 6 & 7 accordingly and then the sensor output is connected to pin 8 then we go and define three variables to measure the pulse width of red green and blue because that's what we are going to be measuring the calibrate and we'll initialize those as zero now in the setup we set up s0 to s3 all as being outputs and the sensor output is an input into the Arduino so we set it accordingly we're going to set the pulse width scaling to 20% because that means it will be in the range that the Arduino can easily measure and then we will set up our serial monitor which we're going to be using to display the results I've set it up at 9600 baud you can change that if you wish just make sure your serial monitor matches now before we get into the loop we'll go down and read one of the functions that actually reads the pulse width so this is get read PW we set the sensor to read the read only in this case by setting s 2 and s 3 both low and this is the only difference by the way between get read PW get green PW and get blue PW otherwise they're identical we define an integer that represents the pulse width that we want to read and then we read that pulse width with an Arduino pulse in command the low at the end of this means we're reading the amount of time that the pulse stays low and we'll assign that to the value PW whoops we return and exit the function and the green and the blue ones are identical with the exception of setting the sensor values here to read green only or to read blue only now up in the loop in the loop we are going to assign the values read PW to the value of the get read PW function we'll put in a slight delay to stabilize the sensor then we'll read the green we'll do the same thing and read the blue and then after that word is gonna simply print our outputs out to the serial monitor so is this quite a simple sketch and it's going to allow us to calibrate the sector so let's go and do some calibration right now alright well we're ready to calibrate our TCS two-30 sensor so as you can see I've got the sensor mounted facing down and I've got a pen and paper here ready to take some calibration values and I also have some color samples now these color samples are by no means professional is just some paper I picked up at the stationery store so they're far from perfect and also I want you to keep in mind that the ambient lighting in the workshop is going to affect this the LED lighting this will work better in the dark but of course that wouldn't have made for a very interesting video so here's my color samples this as you can see is a white sample and I've got a black sample or as close to black-and-white as I can get I've also got a red what I personally think is closer to orange than red I think my shirt is closer to red I also think my meter probe is closer to red but it's going to have to do I've got a green and I have a blue and so we'll start off by placing the white sheet underneath the sensor and we'll take a look at our serial monitor now you can see some of the values we're getting on the monitor right now what we're looking for is the lowest possible value and I've seen some values here down I saw 41 I thought earlier there's a 43 yeah I'm gonna put 43 because that seems to be both the lowest one we're getting right now for red and for green there was a 59 in there 58 58 seems to be the lowest value and for blue I saw 54 [Music] forty-eight there's a 48 there let's call us another 48 let's call that one 48 and now we're gonna look for the highest values and we will put the black Thunder here now remember this is not frequency this is pulse width so it's the inverse of frequency so a larger number means a lower frequency and were looking for the highest number we can get right now I saw 171 172 over there let's call it 172 and for green what's the highest that we're going to get to 46 this went by I'm gonna color to 46 and finally for the blue one 85 was up there 184 186 let's call it 186 okay so now I've recorded the values of the lowest and highest values that I'm expecting to get for red green and blue and I'm going to use that value in our next sketch which is actually going to convert these to RGB values now this is a sketch that we'll be using to extract RGB values from the color sensor and a lot of it is similar to the calibration sketch it starts off identity by defining the pins that we've connected the color sensor to the Arduino with and then we have to enter the calibration values that we received during our previous calibration experiment and so you're going to need to replace all of these zeros with your actual calibration values now I'm going to do that right now the bear in mind that my calibration values won't be the same as yours so you really need to run the first sketch okay the red min that I got was 43 and the red max that I received was 1 72 the minimum green pulse-width was 58 and the maximum was 246 and finally for the blue I had 48 as my minimum and 186 as my maximum okay and again you'll want to modify that with your own calibration values now once again we set up the three variables to measure the pulse width for the three different colors we also have some integers that represent the red green and blue values these there are RGB values that we want to produce with the sketch now the setup is pretty well identical we set the s0 through s3 as outputs we set the sensor output as an input we set our frequency scaling to 20% and we are going to be using the serial monitor again at 9600 or whatever speed you would like does match that with your serial monitor now we're going to use the same functions over here if it get read PW get green PW and get blue PW so it won't go through the begin because we've already done that we'll go into the loop right now and for the loop we will read the read value with get read PW as we did before only this time we're going to map it to a value of 0 to 255 to get the actual RGB value for the read and so we're going to take read value and map that to the red pulse width with red men and red max and notice that we're inverting it right now so the minimum is going to go down to 255 and the maximum up to 0 and that's because we're talking about pulse with the not frequency and pulse width is the inverse of frequency and then the delay to stabilize the sensor and we'll go through and do that for the green and the blue identically and then we'll just print those values out to the serial monitor so with the mapping these could represent closely the red green and blue values or RGB value of the color that works so now that we've looked at this sketch let's go and give it a test so now we're running our test sketch with the calibration values and I've got the white under here now perfect white should be three values of 255 and we're not getting three values at 255 although we're coming very close to those values over here so that's encouraging and we'll put the black on under here and of course a perfect black would have values of all 0 and again it isn't all 0 but again we're getting some very low values over there so that does seem to be responding and once again these are not perfect black or white now I'll take the thing that I'm calling red which as I said is not the reddest of reds and what we should see is that the red should be the highest number and indeed that does seem to be the case over here that the red is much higher than the green and the blue and looks like a green and as we would expect the green is the highest component that we're getting right now higher than the red in the blue and finally we'll put a blue underneath here and the blue is the highest there's not very much red component a little bit green component and again of course these aren't perfect color samples and the ambient light is affecting it so as you can see this is a color sensor that does work it is not the most accurate one but it can indeed distinguish between the different shades of red green and blue and you could indeed use this for one of your projects so as you've seen the TC s 2 3 0 is a usable color sensor but it does leave a little bit to be desired if you calibrate it correctly though you can certainly find a lot of use for it now the next sensor I'm going to look at is a little easier to use because it uses the i2c bus it's the is l-29 1 to 5 sensor and I'm going to be using a module from Sparkfun who have also provided libraries for using this device so let's go and take a look at that color sensor down now we'll look at the is l-29 125 module this is an RGB color sensor module made by sparkfun the module uses an inter cell is l-29 125 RGB sensor with an integrated infrared filter it has an internal analog to digital converter and light processor the module communicates using 3.3 volt I to see it also provides a programmable interrupt output here are the pin outs of the module we have the ground pin the 3.3 volt power supply the i2c data or SDA line the i2c clock or SCL line and the interrupt output you can see the RGB sensor in the center of the module this module is not 5-volt tolerant the module has internal pull-up resistors if you wish to remove the pull-up resistors who cut the two traces illustrated here to disable them the is l-29 one to five has internal registers that can be programmed to customize a device these registers are used to program the function of the interrupt the module provides a 16-bit data output the i2c address of the module is fixed at hexadecimal for four so now let's start using the ISL 2 9 1 2 5 so this is a Sparkfun ISL to 9 1 to 5 module and as you can see it's a very basic module it's got some connections on one side over here and they are all labeled on both sides of the board it's got the sensor right in the center is really the meat of this whole thing and it's a very very tiny sensor as you can see on the other side of the board there really isn't anything except these solder traces that you can cut if you want to not use the pull-up resistors and so there you go a very basic module that we will be using for our experiments so let's see how we can hook this up to an Arduino now here's what you'll need for your experiments with the ISL 2 9 1 2 5 you'll need an Arduino Uno a two-channel logic level converter and the ISL to 9 1 to 5 RGB sensor optionally you can also add 4 white LEDs and for dropping resistors for those LEDs I used 330 ohms but any value from 150 to 470 will work just fine these LEDs are optional and are being used to illuminate the surface and you can use an external light instead if you wish we begin by connecting the 5 volts in the Arduino to the 5 volt on the logic converter well connect the ground from the Arduino to the logic converters ground and the 3.3 volts from the Arduino to the 3.3 volt input on the logic converter will also connect the 3.3 volts to the 3.3 volt input on the is l-29 1 to 5 next we'll connect pin a4 which is the SDA to one channel of the logic converter that Cannell's output will be connected to the SDA input on the color sensor pin a5 or SC l will be connected to the other channel on the logic converter and its output will be connected to the SC l input on the ISL to 9 1 2 5 will also connect the ground from the logic converter to the ISL to 9 1 2 5 s ground if you're using the LEDs connect one side of each resistor to the 5 volts in the Arduino connect the other side of that resistor to the anode on each of the LEDs and then connect the cathode of all the LEDs to the ground and this completes our wiring now in order to work with the sensor we're going to require a library the spark fund is kindly provided for the ISL to 9 1 to 5 and you'll find the length of that library in the article accompanying this video the library is available as a zip file so we'll need to install that into your library manager you can do that by going to sketch and then going in to include library and then add zip library this will open up a dialog box and you should navigate to where you've downloaded your library in my case my downloads folder and look for the library this is it over here now you would hit OK to install it I'm not going to do that because I've already installed it into my IDE once you've installed the library there will be a couple of sample files and we're going to run one of those files to start off with so go down into file and go to examples and scroll down to the examples for custom libraries section and go down until you find the library which is this one over here now you'll notice there are three sample codes that they provide for you the basics the interrupts and the RGB LED we're going to be writing the basics but I'll tell you what the other to do the interrupts is an interesting sketch in which it creates an interrupt when a specific color has been detected and this could be very useful in one of your applications if you want to trigger on the presence of a certain color the RGB LED is also a very interesting sketch what it does is it takes an RGB LED and it cycles it through the three colors red green and blue you shine this on the sensor and you look at the readings that you get on the sensor in a way it allows you to calibrate your sensor we're going to calibrate it as well but I'm going to be using the basic sketch so let's just open that now this is actually a very simple sketch thanks to the use of library we start off by including the wire library which is the library that's included in your Arduino IDE for working with i2c then we include the library that Sparkfun is provided for the sensor we declare an object for that sensor and then move into our setup we're going to be using the serial monitor so we initialize it and note the baud rate that we're using 1 1 5 200 that's higher than you may have your set so make certain when you use your serial monitor you set it to that bod rate and then we go and initialize a sensor itself which is just done with this init command and we print to the serial monitor that it has been initialized and now we go into the loop and as you'll see this is very simple code we read the sensor values using the library so we do a read read read sorry read read read green and read blue and assign it to three different integers now these are going to be 16-bit values and not the 8-bit values that we've used with some other integers and we're using an unsigned integer for that and then we simply print those values out to the serial monitor now right now it is printing out in hexadecimal I'm going to change this to decimal by writing Dec and over here because I find that easier to work with being a human who has 10 fingers and 10 toes and so I'm going to change it to Dec but you can leave it as hexadecimal but I'm going to be using this sketch in order to calibrate my sensor in a similar fashion that I did with the TC s 2 3 0 and so the decimal reading will be a bit more useful to me but that's basically all there is to the sketch just load it up and run it and open your serial monitor making certain to set the baud rate correctly and you should see the results which we're going to do right now and here we have ladies and gentlemen a first and the drone bot workshop the upside-down solderless breadboard test and I've mounted everything on the solderless breadboard as you can see under here and then mounted it in a similar fashion to the way that we tested the TCS two-30 so that I can perform essentially the same experiments and I've got the serial monitor running now in this particular case we are not reading pulse width we are actually reading light intensity that is what the sensor is giving us and the sensor is of course sensitive morr in certain ranges of light than other ranges which is why these aren't even but I'm going to put the white piece of paper underneath it and you can see the numbers that we're coming up with now keep in mind these are 16-bit numbers so in theory we could go as high as 65535 and as low as zero but in practical purposes we'll never get to either of those extremes now let's try the black sheet of paper and as you'll see there readings are much much lower now you can use the exact same process we did with the last sensor to do a calibration between the white and the black and take the maximum and minimum ranges and that is exactly what I am doing now I'm not going to repeat that with this right now and write them down because you've already seen it before but I will gather the numbers and I'll use them for the next sketch that we will use with this sensor now here's the sketch that I've created in order to use the is l-29 one two five to display RGB values now we're going to start off by including the wire library the library for i2c and then we'll include the sparkfun library for the ISL to nine one two five will create an object called RGB sensor to represent our sensor and then we will enter our calibration values now I've entered the values that I obtained for the low and the high for red green and blue respectively and I used the same method that I used for the TCS two-30 now of course when you do this you are going to want to change these values to the values that you obtain when you run the basic program from Sparkfun and get your values in decimal then we're going to also create some integers for the RGB values themselves red value green value and blue value Lanisha lies those with values of zero we're going to set the serial communications as we did before I kept the moderate the same disc because I don't want to go back and change my serial monitor and we have the same initialization routine that we did before we'll do a sensor in it and then we'll print that the initialization was successful now we go into the loop and in the loop we will read the sensor values as we did in the basic sketch and assign them to integers Called red green and blue and again these are 16-bit values so now we're going to convert these values into RGB values and we're going to do this in two steps the actual conversion we'll use the map function and you're now familiar with the map function we'll take the red low and red high and convert them the values of 0 to 255 now notice when you compare this to the previous sketch that we don't have they're reversed them because we're getting color intensity values and not pulse width values like we did with the TCS to 3-0 now after that comes a command that you may not be familiar with the constrain command and the constraint command constrains the value of red green and blue to the range of 0 to 255 now you might wonder why do you need to do that and the reason is that the map command despite what you may think can actually produce values lower than 0 and higher than 255 if we get values that are lower than the red low value are higher than red this can happen with this sensor the constraint command will constrain everything within the range of 0 to 255 so anything that goes below 0 will become a zero anything that goes above 255 will be 255 now you might wonder why I didn't put all of these into the same command simply put constrain all around this statement over here and the reason is you are not supposed to do that with a constraint command you will get odd values if you do you need to use this command separately and you can see the link in the article accompanying this video to the article on the Arduino website that explains this command in great detail after that it's just a simple matter of going to the serial monitor and printing the red green and blue values and adding a delay for the sensor to stabilize and then the loop repeats itself so now let's go and take a look at our RGB displays from the ISL to 9 1 2 5 alright so now let's test our sketch to display RGB values so we have the serial monitor up and we're going to bring our white piece of paper in and this is our white sample and yes it looks very very white in fact it's almost 255 all the way across a little low on the blue side and that's quite good now we'll substitute our black piece and see what we got here and that's very very black as well too it's pretty well down near the bottom before or 2 and a 1 quite nice and our red and most definitely the red is the highest component in this this seems to be a lot more positive values on the top with the previous sensor our green and green is the highest value over here the values seem to be staying pretty steady as well too and finally the blue and it does seem to work on the blue as well so this seems to give better RGB values than the TCS two-30 does and I would say that if you're going to build a project with a color sensor this would be the sensor to use and so there you go a couple of simple ways of adding color sensing to your application now why would you want to use color sensing in the first place well color sensing is used in a number of industrial and quality control applications for example grading fruits and vegetables but if you don't want to grade fruits and vegetables there's still a lot of things you can do with color sensing you could for example make a special lock for your room and only let people with nice red shirts like myself into the room or you could keep people with red shirts like myself out of the room if that's what you wanted to do you could make of course a Rubik's Cube solver and sense all the different colored squares on the side of a Rubik's Cube if you're a jelly bean connoisseur like myself you will know that the black licorice flavored jelly beans are the best ones and you could separate those out from the rest of the jelly beans and I'm sure you can find a number of other interesting applications for color sensing now if you want to discuss color sensing more the best place to do that is on the drone bot workshop forum where you'll find a lot of people just like you discussing color sensing robotics electronics model railways and all sorts of things the forum is also the place to go if you would like to suggest new videos for the channel speaking of the channel if you're not a subscriber yet please do subscribe to the channel just hit the little subscribe button or the robot in the corner of this video and you will be subscribed to make sure to hit the bell notification when you do subscribe and that way you'll be alerted to whenever I make new videos if you want to learn more about what's going on in the drone bot workshop subscribe to the drone bot workshop newsletter you'll find the link to that below this video and you will also find a link to an article that accompanies this video if you like to learn more about the two color sensors that we talked about today so until the next time we get together please take care of your cells please go send some color and I will see you again very soon here in the drone bot workshop goodbye for now [Music]
Info
Channel: DroneBot Workshop
Views: 63,395
Rating: 4.955873 out of 5
Keywords: color sensor, arduino color sensor, TCS230, ISL29125, color sensing arduino, arduino uno, arduino project, arduino color sensing, color sensing, arduino color, arduino color sensor calibration
Id: MwdANEcTiPY
Channel Id: undefined
Length: 34min 56sec (2096 seconds)
Published: Sun Jan 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.