0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

颜色传感器DIY图解

454398 来源:wv 2019-08-30 10:57 次阅读

第1步:BoM

Arduino

RGB LED

光敏电阻

10kΩ电阻

3x100Ω电阻器

跳线

面包板电线

步骤2:连接RGB LED

这将是我们电路的发射器部分发出不同的颜色,这些颜色将从物体反弹,通过光学定律将被检测到我们的光传感器

*将引脚2,最长引脚连接到Arduino上的GND引脚。

*连接引脚1, R GB的红色LED LED指向Arduino上的引脚5。

*将引脚3,R G B LED的贪婪色LED连接到Arduino上的引脚6。

*将引脚4,RG B LED的蓝色LED连接到Arduino上的引脚9。

您会注意到所有这些都插入标有tilda符号“〜”的PWM引脚,这样我们就可以独立控制每个LED的亮度。

步骤3:连接光电传感器

来自发射器(RGB)LED的反射光从中弹回光传感器将读取任何物体,光电传感器将使用校准值来找到特定颜色的各个RGB颜色值。

确保将光传感器移近发射器。

*将其中一个引脚(称为光纤传感器的引脚1)连接到Arduino上的GND引脚

*将光电传感器的引脚2连接到上面的3.3V引脚Arduino的。

*将光电传感器的引脚2连接到Arduino上的A0引脚。

你会注意到最后两条接线都是平行的。这是因为我们正在制作一个分压器,以便在反射光强度发生变化时获得变化的电压读数。

步骤4:代码

// Define colour sensor LED pins int ledArray[] = {5,6,9}; // boolean to know if the balance has been set

boolean balanceSet = false; //place holders for colour detected

int red = 0;

int green = 0;

int blue = 0; //floats to hold colour arrays

float colourArray[] = {0,0,0};

float whiteArray[] = {0,0,0};

float blackArray[] = {0,0,0}; //place holder for average

int avgRead; void setup(){

//setup the outputs for the colour sensor

pinMode(2,OUTPUT);

pinMode(3,OUTPUT);

pinMode(4,OUTPUT);

//begin serial communication

Serial.begin(9600); }

void loop(){ checkBalance();

checkColour();

printColour();

}

void checkBalance(){

//check if the balance has been set, if not, set it

if(balanceSet == false){

setBalance();

}

}

void setBalance(){

//set white balance

delay(5000); //delay for five seconds, this gives us time to get a white sample in front of our sensor

//scan the white sample.

//go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array

for(int i = 0;i《=2;i++){

digitalWrite(ledArray[i],HIGH);

delay(100);

getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well.

whiteArray[i] = avgRead;

digitalWrite(ledArray[i],LOW);

delay(100);

}

//done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample.

//set black balance

delay(5000); //wait for five seconds so we can position our black sample

//go ahead and scan, sets the colour values for red, green, and blue when exposed to black

for(int i = 0;i《=2;i++){

digitalWrite(ledArray[i],HIGH);

delay(100);

getReading(5);

blackArray[i] = avgRead;

//blackArray[i] = analogRead(2);

digitalWrite(ledArray[i],LOW);

delay(100);

}

//set boolean value so we know that balance is set

balanceSet = true;

delay(5000); //delay another 5 seconds to let us catch up

} void checkColour(){

for(int i = 0;i《=2;i++){

digitalWrite(ledArray[i],HIGH); //turn or the LED, red, green or blue depending which iteration

delay(100); //delay to allow CdS to stabalize, they are slow

getReading(5); //take a reading however many times

colourArray[i] = avgRead; //set the current colour in the array to the average reading

float greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between

colourArray[i] = (colourArray[i] - blackArray[i])/(greyDiff)*255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned

digitalWrite(ledArray[i],LOW); //turn off the current LED

delay(100);

}

}

void getReading(int times){

int reading;

int tally=0;

//take the reading however many times was requested and add them up

for(int i = 0;i 《 times;i++){

reading = analogRead(0);

tally = reading + tally;

delay(10);

}

//calculate the average and set it

avgRead = (tally)/times;

}

//prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works.

void printColour(){

Serial.print(“R = ”);

Serial.println(int(colourArray[0]));

Serial.print(“G = ”);

Serial.println(int(colourArray[1]));

Serial.print(“B = ”);

Serial.println(int(colourArray[2]));

//delay(2000);

}

步骤5:校准

首先准备一张黑白纸上传代码。

上传代码后,您会注意到在程序运行的前5秒内,RGB LED会发出各种颜色。在前5秒钟,在LED和光电传感器上放置一张黑纸。然后在接下来的5秒钟内将纸张切换到白纸上。

编写代码,使前10秒为校准周期。

第6步:测试并享受!

取出不同颜色的纸张并进行测试。它会将各个R,G,B值打印到屏幕上。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 颜色传感器
    +关注

    关注

    2

    文章

    83

    浏览量

    18008
收藏 人收藏

    评论

    相关推荐

    阿童木颜色传感器在车身颜色识别中的应用

    传感器
    阿童木(广州)智能科技有限公司
    发布于 :2024年03月12日 15:16:16

    阿童木颜色传感器应用:汽车车身颜色识别

    引言 随着科技的不断进步,汽车制造业也在不断迭代升级,全自动化生产技术成为现代汽车生产的主流。本文将深入探讨某大型汽车生产企业在其全国生产基地中,采用阿童木颜色传感器(CL2)对车身颜色进行识别
    的头像 发表于 01-26 14:45 168次阅读
    阿童木<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>应用:汽车车身<b class='flag-5'>颜色</b>识别

    阿童木颜色传感器应用汽车车漆颜色判断

    传感器应用
    阿童木(广州)智能科技有限公司
    发布于 :2024年01月26日 14:32:03

    阿童木颜色传感器:黑暗环境的检测效果演示

    传感器
    阿童木(广州)智能科技有限公司
    发布于 :2024年01月17日 14:07:30

    阿童木颜色传感器:识别面条包装扎带是否缺失# 颜色传感器 # 面条生产

    传感器
    阿童木(广州)智能科技有限公司
    发布于 :2023年11月07日 13:52:02

    液体颜色是否会影响光电液位传感器的判断

    液体的颜色通常不会对红外水位开关的工作产生影响。红外水位开关是一种非机械式的传感器,它内置了红外发射管和接收管。红外发射管会发射出红外光,而接收管则用于接收光信号。当红外光照射到液体表面时,光会
    的头像 发表于 09-06 13:30 317次阅读
    液体<b class='flag-5'>颜色</b>是否会影响光电液位<b class='flag-5'>传感器</b>的判断

    颜色、色标模式于一身的ESE系列传感器

    什么是颜色传感器颜色传感器(ColorSensor)是用来识别和比较物体表面颜色值的传感器,以
    的头像 发表于 08-22 08:26 451次阅读
    集<b class='flag-5'>颜色</b>、色标模式于一身的ESE系列<b class='flag-5'>传感器</b>

    颜色传感器的原理、分类及操作规程

    颜色传感器是一种能够检测物体颜色传感器。它通过感知物体反射、透射或发射的光的光谱特性,来确定物体的颜色
    的头像 发表于 07-09 11:37 1794次阅读

    颜色传感器的原理

    颜色传感器 何谓颜色传感器? 感光传感器(光传感器)中,检测R(红色)、G(绿色)、B(蓝色)3
    的头像 发表于 06-30 10:18 1542次阅读
    <b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>的原理

    颜色传感器原型:从对象中检测颜色名称

    电子发烧友网站提供《颜色传感器原型:从对象中检测颜色名称.zip》资料免费下载
    发表于 06-28 09:18 8次下载
    <b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>原型:从对象中检测<b class='flag-5'>颜色</b>名称

    TCS3200颜色传感器模块原理图及参考代码

    TCS3200颜色传感器模块参考代码原理图文档资料
    发表于 05-25 15:51 1次下载

    颜色传感器:识别萃取物分界点和颜色分层输出信号提示

    传感器
    阿童木(广州)智能科技有限公司
    发布于 :2023年05月15日 15:50:57

    颜色传感器颜色识别传感器颜色感应器在锂电制片机上的应用

    颜色传感器是一种广泛应用于自动化控制系统中的传感器,可以检测物体的颜色,用于对物体进行分类和识别。随着科技的不断发展,颜色
    的头像 发表于 05-15 15:28 785次阅读
    <b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>丨<b class='flag-5'>颜色</b>识别<b class='flag-5'>传感器</b>丨<b class='flag-5'>颜色</b>感应器在锂电制片机上的应用

    颜色传感器工作原理和特点

    颜色传感器 DK50-UV-609/79d/115b/147 在包装和印刷过程中的自动化,可靠地检测印刷和色彩标记在协调各种处理步骤中的关键作用。光电式对比传感器是为这些检测任务专门设计的。 不同于
    的头像 发表于 04-27 09:53 1005次阅读

    光纤传感器如何识别颜色

    光纤传感器是一种基于光学原理的传感器,它可以通过光的反射、散射等方式来检测物体的各种属性。其中,光纤传感器可以通过不同的颜色反射光线的特性来识别颜色
    的头像 发表于 04-18 17:27 1231次阅读
    光纤<b class='flag-5'>传感器</b>如何识别<b class='flag-5'>颜色</b>?