Skip to content
Home » Mh Sensor Series Flying Fish Arduino Code | Sơ Lược Về Cảm Biến Ánh Sáng Quang Trở (Cds)

Mh Sensor Series Flying Fish Arduino Code | Sơ Lược Về Cảm Biến Ánh Sáng Quang Trở (Cds)

How does work IR SENSOR with Arduino nano | IR SENSOR full tutorial [Codes and Circuit diagrams]

I have an IR (Infra-red) sensor. It seems to always detect an object, at ALL times, no matter how far an object is. I’m pretty sure it can’t detect anything further than a few inches, and yet even when the nearest object is nearly a meter away, it’s LED lights up to show that it has detected an object. I tried playing with its onboard screw (you can easily find it in the attached picture) to change its detection distance, but no avail. I always connect the ‘OUT’ pin to any PMW pin, and ‘VCC’ to +5V and ‘GND’ to ground (also find attached circuitry for more clarity). I’ve never used a resistor with it, although it always worked even then.

Does anybody have an idea on what’s wrong? Possibly a short-circuit?

groundFungus:
Please post your code. Does it make a difference if you shield the sensor from ambient light?

int LED = 5; // Use the onboard Uno LED
int isObstaclePin = A0; // This is our input pin
int isObstacle = LOW; // HIGH MEANS NO OBSTACLE
void setup() {
pinMode(LED, OUTPUT);
pinMode(isObstaclePin, INPUT);
Serial.begin(9600);
}
void loop() {
isObstacle = analogRead(isObstaclePin);
if (isObstacle == HIGH) {
Serial.println(“OBSTACLE!!, OBSTACLE!!”);
digitalWrite(LED, HIGH);
} else {
Serial.println(“clear”);
digitalWrite(LED, LOW);
}
}

Changing the lighting in any way does nothing. I don’t think it will, seeing as all this does is send an infrared signal and then receives it, and lets me know if it detects an obstacle. I also doubt it’s due to the code, but here it is.

Hi,
Can I suggest you build this circuit.
And try this code;

/*
IR Proximity Sensor interface code
Turns on an LED on when obstacle is detected, else off.
blog.circuits4you.com 2016
*/
const int ProxSensor=2;
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//Pin 2 is connected to the output of proximity sensor
pinMode(ProxSensor,INPUT);
}
void loop() {
if(digitalRead(ProxSensor)==HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on
}
else
{
digitalWrite(13, LOW); // set the LED off
}
delay(100); // wait for a second
}

Then sit down and look at how it is structured and works.
This is the link to where a google search took me.

TomGeorge:
Hi,
Can I suggest you build this circuit.
And try this code;

/*

IR Proximity Sensor interface code
Turns on an LED on when obstacle is detected, else off. blog.circuits4you.com 2016
*/

const int ProxSensor=2;

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//Pin 2 is connected to the output of proximity sensor
pinMode(ProxSensor,INPUT);
}

void loop() {
if(digitalRead(ProxSensor)==HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on
}
else
{
digitalWrite(13, LOW); // set the LED off
}
delay(100); // wait for a second
}

Then sit down and look at how it is structured and works.
This is the link to where a google search took me.
http://blog.circuits4you.com/2016/04/arduino-ir-proximity-sensor-interfacing.html
Tom… 🙂

I tried it, and it didn’t work. Do you have an idea on what might be the problem with the sensor itslef?

agrp87132:
Also I am pretty sure allanhurst is correct.

It is just a cheap ($.50) digital sensor.

No way of reporting a distance.

Or distinguishing flying fish.

I have 6 of these to use as obstacle detectors, and they work fine for that.
For adjustable range they are pretty useless.
Tom…
PS, have your sensor looking upward with no obstacles and adjust the pot until the LED just goes out.
The other LED should remain ON as it is the power indicator LED.
Then when you put your finger in range the LED should go ON.

NickSpeed:
I tried it, and it didn’t work. Do you have an idea on what might be the problem with the sensor itslef?

In the picture you posted the solder joints on the LED & IR sensor look suspect. They’re probably OK but,… If you have a soldering iron you may want to touch them up. If you have a digital camera you can see if the LED is operating. Point the camera at the sensor and the LED should shine brightly. If you have a voltmeter handy you could measure the voltage across the sensor terminals and look for a fluctuation when the sensor sees/doesn’t see IR light.

NickSpeed:
I didn’t understand what Allanhurst meant. I understand the sensor is cheap, but I have no idea when I can get a new one

Please also read post #13.
What @allanhurst and I mean is, the sensors ARE NOT designed for absolute DISTANCE measurement, they are OBSTACLE detection sensors.
The output changes when an object appears in front of them, you cannot get any variable range information from the sensor.
You may have a faulty unit, but a new one will not necessarily do what you need it to do.
Tom…
PS. A picture of your project may help, so we can see your component layout.

Because 50% of what the sun emits is IR radiation. Both LEDs lit because the collector photodiode senses that radiation.

The sensors work well under fluorescent lights or in the dark. If you have incandescent or heat lamps it will sense that IR as well and both LEDs will be lit.

If you turn the pot all the way ccw the emitting diode should be off. You can check this with most cell phone cameras, IR appears purple. If you turn pot all the way clockwise, you should see purple with cell phone camera, if you go ccw all the way, no purple. If both are purple, need a new sensor.

Hardware components
Software apps and online services

There are a number of different sensors for measuring light available on the market. it is mainly about the brightness of ambient light, sometimes also about invisible infrared light from cheap to expensive.

Today we take a look at the light sensor from the Flying Fish series from MH: Here, a conventional LDR photoresistor is applied to a breakout board and equipped with comfort functions such as resistors and control lights. In addition to the 4 leads, there is also a potentiometer.

So you have the choice between the state measurement: it is light or dark on the D0-lead and an analog measurement of the voltage on the A0-lead.


// read the input on analog pin 0:

int sensorValue = analogRead(A0);// Convert the analog reading (which goes from 0 – 1023) to a voltage (0 – 5V):float voltage = sensorValue * (5.0 / 1024.0);

You suspect it already: the code is known from the Arduino IDE example sketches and reads in the different voltage on the A0 PIN of the Arduino.


// the lower the voltage, the brighter it is

if ((voltage >= 0) && (voltage <= 0.4)) {Serial.print (“it is light – “);} else if ((voltage > 0.4) && (voltage <= 2)) {Serial.print (“it is bright – “);} else {Serial.print (“it is dark – “);}// print out the value you read:Serial.println(voltage);

You can view the log via the Arduino IDE’s serial monitor:

The following applies to D0:

when sensor pin D0 is connected, the sensor only knows the state light (0.14V) and dark (5.0V). The brightness at which the particular state is to be set can be set using the rotary potentiometer.

Update 05.03.2020

And: yes, the photocell can of course perceive the ambient light and, via the analog pin with the help of the blue potentiometer, you can choose which light (or darkness) the LED strip should be switched on at.

The photos show it as an example with an LED, which I briefly controlled with 6V:

If the ambient light is ‘dark’ via the potentiometer, the green control lamp comes on and the LED lights up.

If I switch on my office lamp, it will be recognized immediately and you can guess it: control lamp and LED go out.

Update 20.03.2020

For the sake of completeness: the LDR sensor can also be used with a 12V consumer.

Embed This Sketch

Use the following HTML code to embed the sketch code above in your blog or website.

Embed The Serial Monitor

You can also embed the Serial Monitor section! Just use this HTML code.

Bật Tắt Đèn bằng cảm biến ánh sáng sử dụng Arduino

What do you think of this article?

Based on chapter 1, we now have a fish balloon, a tank of helium gas, several motors and motor drivers.

DO NOT inflate the balloon yet…

Just leave it like this…

Arduino testing

**If you are familiar with using Arduino, you may skip this chapter.**

Before testing, let’s get to know some of the background to Arduino.

Introduction

Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs, like the lights on a sensor and turn it into output in order to activate a motor, turn on an LED or publish something online. To do so, Arduino programming language and Arduino Software (IDE) will be required.

Types of Arduino

In the Arduino family, there are many different models of boards and modules with different supporting features, for example, Arduino UNO, NANO, MICRO, MEGA. In this project, every model can also perform similar results, it only needs to select the module in your Arduino IDE. We will discuss it in the following part.

Selection of Arduino board

For the design of the product, you are recommended to use Arduino Nano. The pin configuration is as shown below.

Arduino Nano in total provides 22pins for data I/O (input/output), which are D0-D13 and A0-A7. For some pins, like D0, D1, D11, and D12, they serve for other purposes as well. If you do not know what they will do, you should not prioritize their usability.

Nevertheless, Nano is not the lightest or the smallest board in the Arduino series. You have other options regarding the design of the product and you are free to use other boards during development and testing.

Installing the Arduino IDE

You need to install the Arduino Integrated Development Environment (IDE) on the computer. Although your computer might contain the Arduino IDE already, it is recommended to install the Arduino again to reduce the probability of errors during the whole development process. Get your computer ready by working on the following progress.

  1. Go to the official Arduino download page ( https://www.arduino.cc/en/Main/Software )
  2. Select the install packet depends on the operating system of your computer.
  3. A ZIP file will be downloaded to your computer. Unzip it and drag the folder inside to the desktop. Open the folder and open the application “arduino.exe”. Now Arduino IDE is available on your computer.

Setting up the Arduino IDE

  1. Connect the Arduino Nano to the computer using a Nano USB cable. (The version of USB cable depends on the module of Arduino board you are using. Wait until the initialization of the board finishes.
  2. Check the COM Port number of the Arduino Nano in your computer as follows:

    (i) Click “Control Panel” at the Start Menu and click “Hardware and Sound”

    (ii) A window will show up, choose “Device Manager” (under Devices and Printers).

    (iii) If a pop-up message shows up, click ok.

    (iv) A “Device Management” window will show up.

    (v) Double click on “Ports (COM & LPT)”, check the parenthesis next to Arduino Nano. In this case, it is COM3.

  3. Start the Arduino IDE, check the settings:

    (i) Board: Arduino Nano

    (ii) Processor: ATmega328

    (ii) Port: Choose the Port Number obtained in Step 2

    (iii)Programmer: AVRISP mkII

Turning On the On-board Light

This part teaches you more about the interaction between programming instructions, Arduino board, and peripherals, for example, LED.

  1. Click on the “File” > “Examples” > “01. Blink. ” A new window showing a sketch named “Blink” should pop out as below. Just to note that in Arduino IDE, the source code is called a “sketch”.
  2. The pop-out sketch “Blink” should be the same as shown below. Study the comment inside “/* … */” CAREFULLY. One should note that comment is widely used in programming since it provides useful information to people who read the program afterwards. In Arduino, we put the comment inside /* */ or after //. Lines inside /* */ or after // will not be executed by the program.
  3. Verify the sketch “Blink” by clicking the “✔” on the top right-hand corner. If there is no problem, Arduino will show a message as below.
  4. If there are any problems, Arduino will show an error message as below here. If an error message is shown, please try to debug the program by following the message.
  5. If compilation completes without errors, click the “→” to upload the sketch to the Arduino board. The Arduino board will be initialized and be installed with the sketch “Blink” after you upload the sketch.
  6. Wait until the word “Done uploading” shows on the status bar.
  7. Now observe the Arduino board, you should see the on-board LED marked “L” blinking.
  8. We can follow the sketch “Blink” to understand why the on-board LED blinks.

    The program above shows how to set the LED “L” to be the output. There are other settings from Pin 0 to 13 which will be shown in the future part. You HAVE TO set the correct pin as an output so the signal can successfully go out from that pin.

    The program below shows a loop which means the program keeps going through the contents inside until there is any break (this program does not contain break). The instruction “digitalWrite” means giving a signal (HIGH or LOW) to the pin (LED_BUILTIN here). HIGH means applying power while LOW means no power is applied, and “delay” means holding everything. “1000” inside the bracket of “delay” means 1000 milliseconds (ms), which indicates 1000 x 10-3 second, i.e. 1 second.

  9. You can try to change the number 1000 to other amounts, for example, 400 or 2500. The command “delay” then holds 0.4 seconds and 2.5 seconds respectively. Upload the sketch to the Arduino board after you change the number.
  10. DO NOT change any settings. You need to use the blink as the template for completing the next part.

In the next chapter, we will cover, Motor Testing 🙂

You can download the pdf version of this chapter in the Download section below.

Parts in this series

  • Remote Flying Fish Project Part 1: Introduction
  • Remote Flying Fish Project Part 2: DIY Series – Arduino Testing
  • Remote Flying Fish Project Part 3: Motor Testing
  • Remote Flying Fish Project Part 4: Bluetooth Testing
  • Remote Flying Fish Project Part 5: Motor Testing with Remote XY
  • Remote Flying Fish Project Part 6: PCB Design (preparatory)
  • Remote Flying Fish Project Part 7: PCB Design (Schematic)
  • Remote Flying Fish Project Part 8: PCB Design (PCB Layout)
  • Remote Flying Fish Project Part 9: Soldering and Arduino Programming
  • Remote Flying Fish Project Part 10: Flutter Introduction
  • Remote Flying Fish Project Part 11: Flutter Installation on MacOS
  • Remote Flying Fish Project Part 12: Flutter Installation on Windows

In this tutorial, it is shown how to use an IR distance sensor with an Arduino Uno. The todays sensor comes in many names: MH Sensor Series, KY-033 (variant with 3 pins), TCRT5000, etc. Moreover, it is often advertised as IR distance sensor, line tracing sensor or line tracking sensor.In addition to the IR distance sensor, this tutorial makes use of an LCD module called “LCM1602 IIC V1” which is utilized to show sensor values. The main advantage of the LCM1602 IIC V1 is that it is very easy-to-use. For example, it can be controlled by setting up an I2C connection.

List of materials:

Arduino Uno
Jumper wires
Mini breadboard
MH Sensor Series
LCM1602 IIC V1 (LCD)

Remark: Some variants of the module type, such as the KY-033, have only three pins. Typically, the A0 pin is missing. Moreover, the D0 pin is often labeled as S. If you own such a variant, this tutorial is still of use to you. Just ignore the part related to the A0 pin.

Pin layout:

The IR sensor and the LCM1602 module have only four pins. The GND pins of both modules must be connected to the Arduino’s GND pins. The same applies to the VCC pins which must be connected to the Arduino’s 5V pin. As the Arduino Uno has only a single 5V pin, a mini breadboard is used to “split” the 5V pin. Next, the A0 and D0 pin of the IR sensor must be connected to the Arduino. The A0 pin is the raw analog value (0-1023) of the measured distance between the sensor and an obstacle. In this tutorial, A0 is connected to the Arduino’s A0 pin. The D0 pin is a digital pin that goes to HIGH state if the analog value is greater than or equal to a specific threshold. The threshold can be adjusted by the blue trimpot of the IR distance sensor. Here, D0 is connected to the Arduino’s pin 8.As a last step, the LCM1602 module’s SDA and SCL pins must be connected to the corresponding SDA and SCL pins of the Arduino Uno.

Example source code

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de #include

#include LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // initializes the LCM1602 IIC V1 (LCD module) // 0x27 is the I2C address. This address might be different. const int IN_A0 = A0; // analog input const int IN_D0 = 8; // digital input void setup() { pinMode (IN_A0, INPUT); pinMode (IN_D0, INPUT); lcd.begin(16, 2); // begins connection to the LCD module lcd.backlight(); // turns on the backlight } int value_A0; bool value_D0; void loop() { value_A0 = analogRead(IN_A0); // reads the analog input from the IR distance sensor value_D0 = digitalRead(IN_D0);// reads the digital input from the IR distance sensor lcd.setCursor(0, 0); // sets the cursor of the LCD module to the first line lcd.print(“A0:”); lcd.setCursor(3, 0); // sets the cursor of the LCD module to the fourth character lcd.print(value_A0); // prints analog value on the LCD module lcd.setCursor(0, 1); // sets the cursor of the LCD module to the first line lcd.print(“D0:”); lcd.setCursor(3, 1); // sets the cursor of the LCD module to the fourth character lcd.print(value_D0); // prints digital value on the LCD module delay(1000); }

If the code has been compiled and transmitted to the Arduino Uno, the LCD module should show the distance between the IR distance sensor and an obstacle. Keep in mind that the distance is indicated by a analog value between 0 and 1023. Unfortunately, it is very challenging to convert the analog value to a metric unit of length, such as meter or centimeter. The reason is that the measured analog value is strongly influenced by the obstacle’s material. For example, black surface does reflect far less light than white surface. As a consequence, the measured analog value will differ. Interestingly, this characteristic can be used in order to use the IR distance sensor as a “black or white” detector. This application can often be found in “car assembly kits” where multiple IR sensors are mounted on the undercar. As a result, the car is capable of following a black line that is drawn on white ground.

The following pictures show distance measurements with black and white material. Although the distance is about the same, the measured analog value (A0) differs strongly:

Video tutorial

Took me a while to find the purpose of this little device I had in the mail recently. it’s a light detection sensor, which I connected to the arduino nano to test its functionality. it servs the amount of light from 0 (very bright) to 1024 (very dark) using the analog pin. To use this device with the ESP8266 you’ll probably need to adapt the voltage and transform it between 0-1V. But for now, it works fine costing around 2 EUR 🙂

void setup () int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor void setup () { pinMode (ledPin, OUTPUT); Serial.begin (9600); } void loop () { sensorValue = analogRead (sensorPin); digitalWrite (ledPin, HIGH); delay (sensorValue); digitalWrite (ledPin, LOW); delay (sensorValue); Serial.println (sensorValue, DEC); }

Output looks like this (analog):

21 -> bright 90 68 63 81 81 83 89 78 85 99 558 897 822 882 864 -> dark

How does work IR SENSOR with Arduino nano | IR SENSOR full tutorial [Codes and Circuit diagrams]
How does work IR SENSOR with Arduino nano | IR SENSOR full tutorial [Codes and Circuit diagrams]

Sơ đồ đấu nối

Các linh kiện cần thiết cho dự án

Tên linh kiện Số lượng Shopee
Arduino Uno R3 Mua ngay
Cáp nạp Mua ngay
Relay 5V/ 1 kênh Mua ngay
Cảm biến ánh sáng Mua ngay
Dây cắm (Đực – Cái) Mua ngay

Bạn sẽ học được gì

  • Có kiến thức cơ bản về Robotics
  • Chế tạo Robot dò đường thông minh
  • Đánh thức nhà khoa học bên trong bạn
  • Tìm hiểu thêm về Robotics, các thuật toán Robot tự động
  • Kiến thức nền tảng để chế tạo các máy móc tự động phục vụ đời sống sinh hoạt, lao động sản xuất
  • Kiến thức để chế tạo sản phẩm, tham gia các cuộc thi khoa học công nghệ trong nước và quốc tế

Code mẫu

/* * This is the Arduino code for Light module for Arduino (basic) This video shows you how to turn an AC light ON during the night using Light sensor and turn it OFF during the day. This code is basic version where digital output of the module is used. // Written for Robojax.com video * watch HC-SR505 Motion Sensor video for details https://youtu.be/qhThpxiXubI * Code is available at http://robojax.com/learn/arduino * // Writeen by Ahmad S. for Robojax.com on // on Freb 10, 2018 at 13:43 at city of Ajax, Ontario, Canada */ #define LIGHT 7 // define pint 7 for sensor #define RELAY 4 // define pin 4 as for relay /* * Permission granted to share this code given that this * note is kept with the code. * Disclaimer: this code is “AS IS” and for educational purpose only. * */ void setup() { // Light LDR Sensor Code by Robojax.com 20180210 Serial.begin(9600);// setup Serial Monitor to display information pinMode(LIGHT, INPUT_PULLUP);// define pin as Input sensor pinMode(RELAY, OUTPUT);// define pin as OUTPUT for relay } void loop() { // Light LDR Sensor Code by Robojax.com 20180210 int L =digitalRead(LIGHT);// read the sensor if(L == 1){ Serial.println(” light is ON”); digitalWrite(RELAY,LOW);// turn the relay ON }else{ Serial.println(” === light is OFF”); digitalWrite(RELAY,HIGH);// turn the relay OFF } delay(500); // Light LDR Sensor Code by Robojax.com 20180210 }

Giải thích Code

Như mọi khi, bước đầu tiên chúng ta đi vào khai báo chân cho từng thiết bị.

#define LIGHT 7 // define pint 7 for sensor #define RELAY 4 // define pin 4 as for relay

Tiếp theo, trong Vòng lặp loop() ta đặt biến L làm giá trị để đọc cảm biến.

Nếu cảm biến ánh sáng quang trở nhận được giá trị là mức 1.

Chú ý: Lúc này cảm biến ánh sáng sẽ nhận giá trị mức 1 khi đang ở môi trường ánh sáng thấp.

Thì lúc này rơ le sẽ đóng và làm đèn sáng và ngược lại.

void loop() { // Light LDR Sensor Code by Robojax.com 20180210 int L =digitalRead(LIGHT);// read the sensor if(L == 1){ Serial.println(” light is ON”); digitalWrite(RELAY,LOW);// turn the relay ON }else{ Serial.println(” === light is OFF”); digitalWrite(RELAY,HIGH);// turn the relay OFF } delay(500); // Light LDR Sensor Code by Robojax.com 20180210 }

How to use IR sensor with arduino? (With full code)
How to use IR sensor with arduino? (With full code)

Sơ lược về cảm biến ánh sáng quang trở (CDS)

Cảm biến ánh sáng quang trở thay đổi điện trở dựa vào cường độ ánh sáng chiếu vào, cảm biến sử dụng Photoresistor nên cho độ nhạy cao, tín hiệu ổn định.

Đọc thêm: Hướng dẫn sử dụng cảm biến âm thanh (Sound Sensor) với Arduino

Ưu điểm

  • Mạch thiết kế nhỏ gọn.
  • Độ chính xác cao.
  • Linh hoạt trong việc điều chỉnh độ nhạy của cảm biến (thông qua biến trở được tích hợp trên mạch).

Lưu ý: Khi xoay biến trở theo chiều kim đồng hồ thì sẽ làm giảm cường độ nhận biết của cảm biến, tức là môi trường phải ít ánh sáng thì cảm biến mới đọc giá trị digitalRead() là 1.

Để hiểu hơn về cách thức làm việc của hàm digitalRed() các bạn xem bài viết này nhé: Xem ngay.

Các chân chức năng

VCC Cấp nguồn dương cho Cảm biến (3V3 – 5V)
GND Nối Mass
D0 Ngõ ra tín hiệu Digital I/O

Keywords searched by users: mh sensor series flying fish arduino code

Tutorial: Mh Sensor Series/Ky-033/Tcrt5000 + Lcm1602 Iic V1 | Uats A&S #13  - Youtube
Tutorial: Mh Sensor Series/Ky-033/Tcrt5000 + Lcm1602 Iic V1 | Uats A&S #13 – Youtube
Mh Thermistor Thermal Sensor Module - Einstronic Enterprise
Mh Thermistor Thermal Sensor Module – Einstronic Enterprise
Mh Thermistor Thermal Sensor Module - Einstronic Enterprise
Mh Thermistor Thermal Sensor Module – Einstronic Enterprise
Infrared Obstacle Avoidance Module For Arduino With Code - Youtube
Infrared Obstacle Avoidance Module For Arduino With Code – Youtube
How To Work Most Commonly Used Ir Infrared Sensors With Arduino - Sritu  Hobby
How To Work Most Commonly Used Ir Infrared Sensors With Arduino – Sritu Hobby
Mq-7
Mq-7 “Flying Fish” Heating Cycle – General Electronics – Arduino Forum
How To Work Most Commonly Used Ir Infrared Sensors With Arduino - Sritu  Hobby
How To Work Most Commonly Used Ir Infrared Sensors With Arduino – Sritu Hobby

See more here: kientrucannam.vn

Leave a Reply

Your email address will not be published. Required fields are marked *