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

    文章

    105

    浏览量

    18947
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    Vishay VEML6046X00高精度RGBIR颜色传感器技术解析

    Vishay Semiconductors VEML6046X00颜色传感器是一款通过AEC-Q100认证的表面贴装高精度16位RGBIR色彩传感器带I^2^C接口。该传感器包括高灵敏
    的头像 发表于 11-14 11:56 388次阅读
    Vishay VEML6046X00高精度RGBIR<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>技术解析

    ‌OPT4060 RGBW高分辨率颜色传感器的技术解析与应用

    Texas Instruments OPT4060 RGBW高分辨率颜色传感器是一款单芯片颜色传感器,能够测量四个通道,每个通道均具有特定设计的光谱响应。四个通道中的三个通道在红色、绿
    的头像 发表于 09-06 14:22 787次阅读
    ‌OPT4060 RGBW高分辨率<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>的技术解析与应用

    颜色传感器的检测原理和应用实例

    颜色传感器是一种能够感知光线的传感器(光电传感器、光传感器的一种)。
    的头像 发表于 09-04 17:13 2825次阅读
    <b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>的检测原理和应用实例

    OPT4048三色XYZ颜色传感器技术解析与应用指南

    Texas Instruments OPT4048三色XYZ颜色传感器是一款单芯片高分辨率颜色传感器,能够测量四个通道,每个通道具有特定设计的光谱响应。四个通道中的三个通道与CIE三色
    的头像 发表于 08-28 10:05 676次阅读
    OPT4048三色XYZ<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>技术解析与应用指南

    贸泽电子开售适用于汽车应用的全新Vishay Semiconductors VEML6046X00颜色传感器

     VEML6046X00颜色传感器。这是一款符合汽车标准的表面贴装、高精度16位RGBIR颜色传感器,配备I2C接口,非常适合汽车应用中的显示
    的头像 发表于 08-14 15:50 1275次阅读
    贸泽电子开售适用于汽车应用的全新Vishay Semiconductors VEML6046X00<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>

    LANBAO兰宝标识类传感器颜色 色标 标签传感器合集#传感器 #兰宝

    传感器
    LANBAO兰宝集团
    发布于 :2025年06月24日 09:50:17

    新品上市丨阿童木CE系列颜色传感器

    导语在工业自动化领域,颜色检测的精度与效率直接影响产品品质与产线效能。阿童木智能科技推出CE系列高速智能型数字颜色传感器,以创新双模式检测逻辑与柔性化工业设计,为多行业提供稳定可靠的颜色
    的头像 发表于 06-11 16:11 1076次阅读
    新品上市丨阿童木CE系列<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>

    颜色传感器识别纺织布条字母定位裁切# 颜色传感器

    传感器
    阿童木(广州)智能科技有限公司
    发布于 :2025年05月29日 14:36:53

    传感器有哪些类型?有哪些接口?

    ,广泛应用于工业控制、汽车制造等领域。 光电传感器:利用光信号进行检测,包括光电开关、反射式光电传感器颜色传感器等。 接近传感器:检测物体
    的头像 发表于 05-22 09:33 1.2w次阅读

    光谱传感器的用途

    光谱传感器是一种高科技设备,它通过测量物体的光谱特征,能够更准确地判断物体的颜色、成分及其他相关信息。光谱传感器的用途非常广泛,涵盖了许多重要领域,以下是对其主要用途的详细归纳:
    的头像 发表于 01-27 15:31 1303次阅读

    光谱传感器可以做什么

    光谱传感器是一种能够检测多种颜色和光谱信息的传感器,通过测量物体的光谱特征,可以实现对物体的颜色、成分等属性的准确判断。以下是对光谱传感器
    的头像 发表于 01-27 14:19 1277次阅读

    【CW32模块使用】TCS34725颜色识别传感器

    本模块是基于AMS的TCS3472XFN彩色光数字转换为核心的颜色传感器传感器提供红色,绿色,蓝色(RGB)和清晰光感应值的数字输出。集成红外阻挡滤光片可最大限度地减少入射光的红外
    的头像 发表于 12-30 11:28 1133次阅读
    【CW32模块使用】TCS34725<b class='flag-5'>颜色</b>识别<b class='flag-5'>传感器</b>

    如何用SS1系列颜色传感器示教多通道颜色

    基于白色LED光源的特性,深视智能SS1系列颜色传感器突破了传统单色RGB、融合光RGB的波长限制,将发射波长的范围覆盖到整个可见光谱,最大支持检测16种颜色。那么,我们如何利用SS1系列颜色
    的头像 发表于 12-23 08:17 1066次阅读
    如何用SS1系列<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b>示教多通道<b class='flag-5'>颜色</b>?

    阿童木颜色传感器 CL4 在数控机床不锈钢钢管检测应用案例

    )。这要求传感器精度高、反应快,且能适应钢管可能的倾斜与距离变化。三、解决方案采用阿童木颜色传感器CL4。其内置白色LED与自定义光接收元件,依目标外观(颜色)检测
    的头像 发表于 12-10 16:08 723次阅读
    阿童木<b class='flag-5'>颜色</b><b class='flag-5'>传感器</b> CL4 在数控机床不锈钢钢管检测应用案例