In this lesson, we will show how to use the photoresistor with an Osoyoo UNO, we will monitor the output of a photoresistor, allow the Arduino to know how light or dark it is. When the light falls below a certain level, the Arduino turns on an LED.
Arduino IDE (version 1.6.4+)
Photocells are sensors that allow you to detect light. They are small, inexpensive, low-power, easy to use and don’t wear out. For that reason they often appear in toys, gadgets and appliances. They are often referred to as CdS cells (they are made of Cadmium-Sulfide), light-dependent resistors (LDR), and photoresistors.
Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how much light is shining onto the squiggly face.When it is dark, the resistance of a photoresistor may be as high as a few MΩ. When it is light, however, the resistance of a photoresistor may be as low as a few hundred ohms. They are very low cost, easy to get in many sizes and specifications, but are very innacurate. Each photocell sensor will act a little differently than the other, even if they are from the same batch. The variations can be really large, 50% or higher! For this reason, they shouldn’t be used to try to determine precise light levels in lux or millicandela. Instead, you can expect to only be able to determine basic light changes.
This graph indicates approximately the resistance of the sensor at different light levels:
You connect the components as shown in the diagram below. Connect the LED to pin 9 of the Arduino. The 200 ohm resistor is current limiting resistor. One lead of the photo resistor is connected to 5V, the other to one lead of the 10k ohm resistor. The other lead of the 10k ohm resistor is connected to ground. This forms a voltage divider, whose output is connected to pin A0 of the Arduino.
As the light impinging on the photoresistor gets stronger, the resistance decreases, and the voltage output of the divider increase. The reverse happens, when the impinging light gets weaker.
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.
You can download the sketch from this link or copy below code to your Arduino IDE window:
int photocellPin = A0; // select the input pin for the photoresistor int ledPin = 9; // select the pin for the LED int val = 0; // variable to store the value coming from the sensor void setup() {Serial.begin(9600); //Set the baudrate to 9600,make sure it's same as your software settings pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT pinMode(photocellPin, INPUT); // declare the ledPin as an OUTPUT } void loop() { val = analogRead(photocellPin); // read the value from the sensor Serial.println(val); //The serial will print the light value if(val<=512) // the point at which the state of LEDs change { digitalWrite(ledPin, HIGH); // set LED on } else { digitalWrite(ledPin, LOW); //set LED off } }
In this experiment, we will connect a photoresistor to an Arduino analog input and read the value with the analogRead() function. Depending on the value the Arduino reads, the program will then set pin 9 HIGH or LOW to turn on or turn off the LED night lights. The threshold value is 512. When the analog value read is less than 512, the Arduino will turn the LEDs on. When the analog value it reads is more than 512, the Arduino will turn the LEDs off.
Open the Arduino IDE and select corresponding board type and port type for your Arduino board.
After compile this sketch, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.
If the room is lighted, the LEDs should not light. Try getting them to turn on it by covering the photoresistor with your hand. Remove your hand and observe that they turn off again.
In the same time, open the Serial Monitor and you will get the output data as below :
Note:
When you are using the Serial Monitor, please make sure the baudrate setting is same as your sketch definition.
In this experiment, we will use eight LEDs to indicate light intensity. The higher the light intensity is, the more the LED is lit. When the light intensity is high enough, all the LEDs will be lit. When there is no light, all the LEDs will go out.
Step 1: Build the circuit
Step 2: Program
You can get the sketch here,or copy below code to your Arduino IDE windows:
const int NbrLEDs = 8; const int ledPins[] = {5, 6, 7, 8, 9, 10, 11, 12}; const int photocellPin = A0; int sensorValue = 0; // value read from the sensor int ledLevel = 0; // sensor value converted into LED 'bars' void setup() { for (int led = 0; led < NbrLEDs; led++) { pinMode(ledPins[led], OUTPUT);// make all the LED pins outputs } } void loop() { sensorValue = analogRead(photocellPin); ledLevel = map(sensorValue, 300, 1023, 0, NbrLEDs); // map to the number of LEDs for (int led = 0; led < NbrLEDs; led++) { if (led < ledLevel ) { digitalWrite(ledPins[led], HIGH); // turn on pins less than the level } else { digitalWrite(ledPins[led],LOW); // turn off pins higher than // the level } } }
Step 3: Compile the code
Step 4: Upload the sketch to the Osoyoo Uno board
Now, if you shine the photoresistor with a certain light intensity, you will see several LEDs light up. If you increase the light intensity, you will see more LEDs light up. When you place it in dark environment, all the LEDs will go out.
A relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate a switch, but other operating principles are also used, such as solid-state relays. Relays are used where it is necessary to control a circuit by a separate low-power signal, or where several circuits must be controlled by one signal.
In this lesson, we will show you how the 2-Channel Relay Module works and how to use it with the Osoyoo Uno board to control high voltage devices.
This is a 5V 2-Channel Relay Module board, Be able to control various appliances, and other equipment with large current. It can be controlled directly by Microcontroller (Raspberry Pi, Arduino, 8051, AVR, PIC, DSP, ARM, ARM, MSP430, TTL logic). Very useful project for application like Micro-Controller based projects, Remote controller, Lamp on Off, and any circuits which required isolated high current and high voltage switching by applying any TTL or CMOS level voltage.
It has a 1×4 (2.54mm pitch) pin header for connecting power (5V and 0V), and for controlling the 2 relays. The pins are marked on the PCB:
There is a second 1×3 (2.54mm pitch) pin header for supplying the “relay side” of the board with 5V. At delivery, a jumper is present on this header selecting the 5V signal from the 1×4 pin header to power the relays. For default operation, don’t change this jumper!
The pins of the 1×3 pin header are marked on the PCB:
If opto isolation is required, an isolated 5V supply should be used. For normal operation, a jumper bewtween pins 1 and 2 selects the 5V signal from the 1×4 pin header. This means both the “input side”, and “relay side” use the same 5V supply, and there is no opto-isolation.
The 2 channel relay module could be considered like a series switches: 2 normally Open (NO), 2 normally closed (NC) and 2 common Pins (COM).
The working of a relay can be better understood by explaining the following diagram given below.
There are 5 parts in every relay:
1. Electromagnet – It consists of an iron core wounded by coil of wires. When electricity is passed through, it becomes magnetic. Therefore, it is called electromagnet.
2. Armature – The movable magnetic strip is known as armature. When current flows through them, the coil is it energized thus producing a magnetic field which is used to make or break the normally open (N/O) or normally close (N/C) points. And the armature can be moved with direct current (DC) as well as alternating current (AC).
3. Spring – When no currents flow through the coil on the electromagnet, the spring pulls the armature away so the circuit cannot be completed.
4. Set of electrical contacts – There are two contact points:
.Normally open – connected when the relay is activated, and disconnected when it is inactive.
.Normally close – not connected when the relay is activated, and connected when it is inactive.
5. Molded frame – Relays are covered with plastic for protection.
Principle
The diagram shows an inner section diagram of a relay. An iron core is surrounded by a control coil. As shown, the power source is given to the electromagnet through a control switch and through contacts to the load. When current starts flowing through the control coil, the electromagnet starts energizing and thus intensifies the magnetic field. Thus the upper contact arm starts to be attracted to the lower fixed arm and thus closes the contacts causing a short circuit for the power to the load. On the other hand, if the relay was already de-energized when the contacts were closed, then the contact move oppositely and make an open circuit.
As soon as the coil current is off, the movable armature will be returned by a force back to its initial position. This force will be almost equal to half the strength of the magnetic force. This force is mainly provided by two factors. They are the spring and also gravity.
Relays are mainly made for two basic operations. One is low voltage application and the other is high voltage. For low voltage applications, more preference will be given to reduce the noise of the whole circuit. For high voltage applications, they are mainly designed to reduce a phenomenon called arcing.
Before we continue with this lesson, I will warn you here that we will use High Voltage which if incorrectly or improperly used could result in serious injuries or death. So be very caution of what you are doing.
In this example, when a low level is supplied to signal terminal of the 2-channel relay, the LED on the relay will light up. Otherwise, it will turn off. If a periodic high and low level is supplied to the signal terminal, you can see the LED will cycle between on and off.
Build the circuit as below digram:
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.
//the relays connect to int IN1 = 2; int IN2 = 3; #define ON 0 #define OFF 1 void setup() { relay_init();//initialize the relay } void loop() { relay_SetStatus(ON, OFF);//turn on RELAY_1 delay(2000);//delay 2s relay_SetStatus(OFF, ON);//turn on RELAY_2 delay(2000);//delay 2s } void relay_init(void)//initialize the relay { //set all the relays OUTPUT pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); relay_SetStatus(OFF, OFF); //turn off all the relay } //set the status of relays void relay_SetStatus( unsigned char status_1, unsigned char status_2) { digitalWrite(IN1, status_1); digitalWrite(IN2, status_2); }
A few seconds after the upload finishes, you should see the LED cycle between on and off.
We need Switch to control electronics or electrical appliances or some thing, Some time electrical switches will give a shock when we use electrical switches with wet hand and then touch to control electrical or electronic load is much interactive than ordinary switches, may be some projects needs touch switch.
In this lesson, we will show what is Digital Touch Sensor Module and how to use it with the Arduino board.
Arduino IDE (version 1.6.4+)
-Control Interface : A total of three pins (GND, VCC, SIG), GND to ground , VCC is the power supply , SIG digital signal output pin ;
-Power Indicator : Green LED, power on the right that is shiny ;
-Touch area : Similar to a fingerprint icon inside the area , you can touch the trigger finger .
-Positioning holes : 4 M2 screws positioning hole diameter is 2.2mm, the positioning of the module is easy to install , to achieve inter- module combination ;
TTP223 is 1 Key Touch pad detector IC, and it is suitable to detect capacitive element variations. It consumes very low power and the operating voltage is only between 2.0V~5.5V. The response time max about 60mS at fast mode, 220mS at low power mode @VDD=3V. Sensitivity can adjust by the capacitance(0~50pF) outside.
Connect Vcc pin of Sensor breakout board to Arduino’s +5V pin and GND to GND. Connect Signal (SIG) pin to Arduino Digital pin D2.
The sketch below provides an output to your serial monitor indicating whether or not the sensor is pressed.
After the uploader , if use finger or metal object touch the metal surface of the transducer , the red LED lights on the UNO will light. Open the Serial Monitor at baudrate 9600, and you will see something as below:
前面测试通过了车体安装,电机能够正常运转。在这一课中将告诉大家如何让小车循迹。
step1:下载软件源码
cd osoyoo-robot/ wget --no-check-certificate http://osoyoo.com/driver/lf-tank.tar.gz
step2:解压源码包 lf.tar.gz里面包含了循迹模块测试程序和循迹程序
tar -zxvf lf-tank.tar.gz cd lf
step3:测试循迹模块 循迹模块上有5个黑白循迹探头,每个探头对应一个红色信号灯,没有检测到黑线时候灯亮起;当检测到黑线时候信号灯熄灭,输出端输出一个低电平,5个探头从左往右分别编号为1-5.将循迹模块探头放置在白色背景黑色线上,当对应探头在黑线上时候,终端会输出提示信息:”No.x
is OK!”,同时,信号灯熄灭。例如,3号探头位于黑线上,会打印出”No.3 is OK!”。如果探头位于黑线上,但是终端输出:”No
black line detected!”,说明循迹模块不正常,请检查接线是否正确;探头是否距离黑线太高或太近。
sudo python test.py
如果测试没有问题,就可以运行下面的循迹程序了。如果测试不通过,请检查循迹模块接线是否正确。
将小车放置在事先铺设好的轨道上,轨道要求白色背景黑色线,线宽在10 cm-30 cm之间,轨道尽量平滑,避免铺设转弯处是直角的轨道。运行下面shell命令后,小车将沿着轨道前进。
sudo python line_follow.py