TCS3200 collor sensor with arduino r3 uno - arduino

I am trying to wire up the TCS3200 color sensor for arduino uno R3 and I am having issues. I have checked many documents on the web and they all have different ways of wiring. Some document set OE pin on TCS3200 to ground on the Arduino, and other don't use the OE pin. Some document set s0= Arduino pin 2, s1= Arduino pin 3, s2= Arduino pin 4, s3=Arduino pin 5, and out pin to Arduino pin 10. Other documents have a different pin wiring configurations.
Hence i was wonder what is the correct way to wire TCS3200 color sensor to the Arduino? I have tried a lot of wiring configuration but the LEDs on TCS3200 won't light up. Does anyone have a solution?
Here is my code:
void setup() {
pinMode(2, OUTPUT); //s0
pinMode(3, OUTPUT); //s1
pinMode(4, OUTPUT); //s2
pinMode(5, OUTPUT); //s3
pinMode(10, INPUT); //out
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
Serial.begin(9600);
}
void loop() {
digitalWrite(4,LOW);
digitalWrite(5,LOW);
Serial.println("red");
Serial.print(pulseIn(10, LOW));
Serial.println();
delay(100);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
Serial.println("green");
Serial.print(pulseIn(10, LOW));
Serial.println();
delay(100);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
Serial.println("blue");
Serial.print(pulseIn(10, LOW));
Serial.println();
delay(100);
}
Thanks

Related

Arduino is freezing even with watchdog enabled

I have made a hardware development based on an ATmega328P and programmed with the Arduino IDE.
The board has one relay output that switchs an AC load.
From time to time, the microcontroller is restarted when the load is switched ON or OFF. No mystery so far. It is probably some EMI interference that is causing the reboot.
BUT, sometimes the microcontroller freezes completely. I canĀ“t figure out why, as I have enabled the watchdog timer. There shouldn't be any freezing. As far as I know, the watchdog timer should restart the microcontroller after 2 seconds.
I would need help understanding WHY I am getting this behaviour and of course, if there could be any software fix.
This is a simple code to show this behaviour. I already tried to change some fuses configurations (brownout, wdton, etc) but no luck so far.
Any help would be much appreciated
Thanks in advance
#include <avr/wdt.h>
#define R0 3
#define R1 4
#define R2 5
unsigned int delayTime = 200;
unsigned int counter = 0;
//--------------------------------------------------------------------
void setup() {
MCUSR = 0;
wdt_disable();
Serial.begin (9600);
delay (1000);
Serial.println ("********************RESTARTING*****************");
pinMode (R0, OUTPUT);
pinMode (R1, OUTPUT);
pinMode (R2, OUTPUT);
delay (2000);
wdt_enable (WDTO_2S);
}
//--------------------------------------------------------------------
void loop() {
wdt_reset();
digitalWrite (R1, HIGH);
counter++;
Serial.print ("R1 activated, counter = "); Serial.println (counter);
delay (delayTime);
digitalWrite (R1, LOW);
Serial.print ("R1 deactivated, counter = "); Serial.println (counter);
delay (delayTime);
}
You not have enabled watchdog from reset for three second. I recomend use WDR activation by fuse bit WDTON, not by sw after three second.
void setup() {
MCUSR = 0;
wdt_disable(); //WDR disabled
Serial.begin (9600);
delay (1000); //one second delay
Serial.println ("********************RESTARTING*****************");
pinMode (R0, OUTPUT);
pinMode (R1, OUTPUT);
pinMode (R2, OUTPUT);
delay (2000); //two second delay
wdt_enable (WDTO_2S);

Arduino program does not work with Proteus

I wrote an Arduino program for the simulation in Proteus. I have an Arduino mega 2560 board and 7-segment cathode. I want the 7-segment to show number "8". I already compiled Arduino program and copied the path of .hex file to put on the board. When I run the simulation, the 7-segment does not light up; however, the simulation was running with no errors. I am very new to this. help me please.
Proteus, Arduino mega 2560, 7-segment cathode
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
digitalWrite(0, 1);
digitalWrite(1, 1);
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
}
Mine worked very well, have you chosen the Atmega2560 processor in Arduino IDE?
This is the picture of a working schematic:
Why isn't it working on your device?
Can you check on proteus pins indicators to see if there is any power or flushing on them. Its a quick to troubleshoot and see if your digitalWrite() functions are indeed writing to those pins

stm32 pin assignment in Arduino IDE

I have a multi-cooker which is not working so I decided to use its PCB for my project. It has an STM32f103c8t7 chip on it. some LEDs and a buzzer and a display, which I may not need though. I have found the connection between chip pins and elements on the board (for example the buzzer is connected to the pin number 13, "PA3") but I have a problem. since this is my first try with stm32 in arduino, I don't know how to name the pins of chip. I have tried PA3, 13 and D13 but none of them seem to to useful. what should I do to make the board buzz?
the buzzer is ok and works fine. Actually one of the buzzer's pin is connected to 12 V and another pin is connected to a transistor which its base is connected to the chip.
this is the code that I have tried:
void setup() {
pinMode(PA3, OUTPUT);
// pinMode(13, OUTPUT);
// pinMode(D13, OUTPUT);
}
void loop() {
digitalWrite(PA3, HIGH);
delay(delayTime);
digitalWrite(PA3, LOW);
delay(delayTime);
// digitalWrite(13, HIGH);
// delay(delayTime);
// digitalWrite(13, LOW);
// delay(delayTime);
// digitalWrite(D13, HIGH);
// delay(delayTime);
// digitalWrite(D13, LOW);
// delay(delayTime);
}

Serial Communication is not working properly in Arduino connection with HC-05

I am new to Arduino Programming.
This code works fine. It gives the correct output and functioning well. But I wanted to write the commands automatically without typing repeatedly.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
if(Serial.available()){
BTSerial.write(Serial.read());
}
}
So i tried doing the following change in the above code but i could not receive any response.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
I also tried BTSerial.print("AT"); But still no response.
AT commands are case sensitive and should end up with a terminator like an enter keystroke or a \r\n.
This should do:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT\r\n");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}

How to make arduino go in two direction using h-bridge?

I am tring to build a line follower robot, i have an h-bridge and arduino to control the direction and speed.
I burnt this code to my arduino board, but the robot just go forward and never go on the oppsite direction:
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoOne()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
demoOne();
delay(1000);
demoTwo();
delay(1000);
}
How to adjust the code to make it go for both directions?
UPDATE:
I am using h bridge l298n and four dc motors "2 right and 2 left", powering it with two 9v batteries and powering the arduino uno with 5v which i get from the h bridge based on this article:
https://hackerstore.nl/PDFs/Tutorial298.pdf
enA, in1, and in2 for the two right motors
enB, in3, and in4 for the two left motors
Your code looks perfectly fine, make sure your hardware is working properly and that everything is connected properly, also make sure that you are not using the 5V from your Arduino to power the motors since the small voltage regulator on the Arduino board is not capable of handling high currents such as 500mA (Unless you replace it with a bigger regulator and give it a big heatsink), if you are powering the arduino and motors from USB make sure your USB port can handle the power required by the two motors, oh and make sure pins 10 and 5 are PWM pins
Also using a dedicated motor library may help to simplify your code, I wrote you one here:
#include <Arduino.h>
#include <stdbool.h>
#define enA 10
#define enB 5
#define i1 8
#define i2 9
#define i3 6
#define i4 7
void initMotors(){
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(i1, OUTPUT);
pinMode(i2, OUTPUT);
pinMode(i3, OUTPUT);
pinMode(i4, OUTPUT);
}
void motor1Stop(){
digitalWrite(i1, LOW);
digitalWrite(i2, LOW);
}
void motor2Stop(){
digitalWrite(i3, LOW);
digitalWrite(i4, LOW);
}
void motorsEnabled(bool en){
digitalWrite(enA, en);
digitalWrite(enB, en);
}
void enableMotors(){
motorsEnabled(HIGH);
}
void motor1Forward(){
digitalWrite(i1, HIGH);
digitalWrite(i2, LOW);
}
void motor1Backward(){
digitalWrite(i1, LOW);
digitalWrite(i2, HIGH);
}
void motor2Forward(){
digitalWrite(i3, LOW);
digitalWrite(i4, HIGH);
}
void motor2Backward(){
digitalWrite(i3, HIGH);
digitalWrite(i4, LOW);
}
To include this just make a new tab and name it whatever you want (as long as there are no spaces and it ends in .h), then add #include "name.h" to the beginning of your code, this will help you to reduce future bugs
For speed control you can add this:
void setMotorSpeeds(int motor1, int motor2){
analogWrite(enA, motor1);
analogWrite(enB, motor2);
}
Your code is fine, so you will need to identify the problem by "divide and conquer"
Connect a single motor to each channel, and forget the arduino part for now.
Connect the L298 power supply. If you read the instructions you will see that you can use the integrated 5V power regulator, or you can supply an external 5V. If using a different power supply for 5V and Motors make sure you connect both GND together otherwise it won't work.
Make sure you have the driver properly connected to power supply, take two cables and use them to connect in1 to GND, in2 to 5V and enA to 5V (without the arduino, just manually connect them with a cable). One of the motors should turn one side.
Now connect in1 to 5V and in2 to GND (keeping enA connected to 5V), the motor should turn to the other side.
If this does not happen then you are not properly connecting the L298 or it is damaged.
If it works then the problem comes from your arduino. Connect 3 LEDs, one in the pin9, one in the pin 8 and one in the pin 10 (which are the pins you connect to in1 and in2 and enA).
Run your program and you should see the LEDS turn on and off accordingly.
If not, your arduino has a problem in some pins (maybe you short circuited them?)
If both experiments work there is absolutely no reason for them to not work together, so start small, divide your problem into pieces and identify it.
Good luck.

Resources