Ultasonic and line sensor

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi all,

   

I am new to the PSoC platform and am having trouble implementing an ultrasonic and line sensor for a robot.

   

The arduino code for each is listed below, any help on how i could implement this code with PSoC would be greatly appreciated.

   

Ultrasonic

   
/* Tested with HY-SRF05, HC-SR04 The circuit:  * VVC connection of the sensor attached to +5V  * GND connection of the sensor attached to ground  * TRIG connection of the sensor attached to digital pin 12         * ECHO connection of the sensor attached to digital pin 13 */ const int TRIG_PIN = 12;const int ECHO_PIN = 13; void setup() {  // initialize serial communication:  Serial.begin(9600);    pinMode(TRIG_PIN,OUTPUT);  pinMode(ECHO_PIN,INPUT);} void loop(){   long duration, distanceCm, distanceIn;    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:  digitalWrite(TRIG_PIN, LOW);  delayMicroseconds(2);  digitalWrite(TRIG_PIN, HIGH);  delayMicroseconds(10);  digitalWrite(TRIG_PIN, LOW);  duration = pulseIn(ECHO_PIN,HIGH);    // convert the time into a distance  distanceCm = duration / 29.1 / 2 ;  distanceIn = duration / 74 / 2;    if (distanceCm <= 0){    Serial.println("Out of range");  }  else {    Serial.print(distanceIn);    Serial.print("in, ");    Serial.print(distanceCm);    Serial.print("cm");    Serial.println();  }  delay(1000);}
   

 

   

Line Sensor

   

    

   

              

   
     //Code for the QRE1113 Digital board   
   
     //Outputs via the serial terminal - Lower numbers mean more reflected   
   
     //3000 or more means nothing was reflected.   
   
    
   
   
        
   
        
   
        
   
        
   

 

   
        
         
            
         
            
         
       int QRE1113_Pin = 2; //connected to digital 2     
       

 

   
        
   
     void setup(){   
   
       Serial.begin(9600);   
   
     }   
   
        
   
     void loop(){   
   
       int QRE_Value = readQD();   
   
       Serial.println(QRE_Value);    
   
     }   
   
        
   
     int readQD(){
   
   
        
   
        
   
        
   
        
   

 

   
        
         
            
         
            
         
         //Returns value from the QRE1113      
       

 

   
        
   
        
   
        
   
        
   
        
   

 

   
        
         
            
         
            
         
         //Lower numbers mean more refleacive     
       

 

   
        
   
        
   
        
   
        
   
        
   

 

   
        
         
            
         
            
         
         //More than 3000 means nothing was reflected.     
       

 

   
       pinMode( QRE1113_Pin, OUTPUT );
   
   
        
   
        
   
        
   
       digitalWrite( QRE1113_Pin, HIGH );     
   
        
   
        
   
        
   
        
   
        
   
        
   
        
   
        
   
       delayMicroseconds(10);   
   
       pinMode( QRE1113_Pin, INPUT );   
   
        
   
       long time = micros();   
   
    
   
   
        
   
        
   
        
   
        
   

 

   
        
         
            
         
            
         
         //time how long the input is HIGH, but quit after 3ms as nothing happens after that     
       

 

   
       while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 3000);    
   
       int diff = micros() - time;   
   
        
   
       return diff;   
   
     }   
0 Likes
14 Replies
Anonymous
Not applicable

 have you created a psoc project?  

0 Likes
Anonymous
Not applicable

 would be helpful if you post your hardware design as well.

0 Likes
Anonymous
Not applicable

Do you actually understand the arduino code?

   

if you do, then it will be pretty straightforward to translate it to psoc funtions. The Arduino code you posted is basically setting pins high and down and using delays to create the pulse and then reading the status of the input pin. But that is not the PSOC way.

   

The problem with the arduino aproach is that you leave the cpu busy and your system unreponsive for the whole 50ms or more that the measurement takes.

   

 

   

take a look here http://www.cypress.com/?app=forum&id=4749&rID=80281

   

I have just posted a project using the HC-SR04 that does everything in hardware.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
        i have a good c code background and am having trouble getting my head around moving to block diagrams i think is my problem. i was trying to do delays with CyDelayUs(). Zeta your example looks like exactly what i have been looking for, i will experiment with this when i get home. my (bad) attempt for the line sensors is attached   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

"Thinking PSoC" takes some experience. We C-coders (I admit here: I am one) usually start by thinking in bits, flags and code. The best way to handle a PSoC project is to start building a schematic. That includes to "know" all (or many) usermodules and what they perform. That takes time.

   

Building up a schematic which is capable of handling most of the required job will not only use less code but will run faster and does not burden the CPU.

   

An example: You may time the input of an ultra-sonic distance-sensor by constantly monitoring a pin or you use a timer with a capture-input and get interrupted. Same as easy is to measure the High-time of a signal, just connect the capture- and the enable-input together and provide the timer with an appropiate clock to get the required granulary.

   

No bit nor byte fiddling, pure hardware.

   

 

   

Hapy wiring

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

And for your line-interface as well:

   

Why not only transmit the information of the line-state when that state did change? Can be detected with hardware and the program can be informed by an interrupt.

   

For the access to pins: Just give a name to your pin and access it in your program with pin_Read() or pin_Write(). Which of the physical connections to the outer world you will specify on a different page within the Creator-software so you are nearly completely free which pin to use without changing schematic nor code.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

And one ´(last) hint concerning the calculation of the distance: Sonic air-speed is mostly related to temperature (not atmospheric pressure!!!) which easily will make up an error of 10 per cent. Seems to be quite a few, but in terms of a dent in a car it could be noticeable.

   

 

   

Bob

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received
        A little warning: Sensor does not hear the echo if the plane smooth but is located at an angle to the axis of the sensor. Recommend: exercise on the toy car)))).   
0 Likes
Anonymous
Not applicable

 We all have been there when we started, but soon after you will "get PSOC" and if you have to work on another mcu you will miss psoc features. lol

0 Likes
Anonymous
Not applicable

 as for the line sensor, I would also use pwm to set up come cicle.  I would recommend using an analog comparator and fire a ISR when there is change from low to high or high to low reflectance. Another aproach is using the ADC where you could ue filtering to get a more robust dsign. Maybe also first measure the analog output from the sensor with the LED turned off to set up a base ambient light/noise floor then set a short pulse to turn the LED and measure the sensor output with the LED on

0 Likes
Anonymous
Not applicable

Hello,

   

Am presently working on CY8C-KIT-042-BLE PIONEER KIT and PRoC. Can anyone help me for the schematic and a hardware design to interface a ultrasonic sensor on to the development board. Kindly help on the same.

   

Thank you,

   

Sunil Kumar 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum, Sunil!

   

This project is for a different kit, but you can get it to work on your BLE pioneer kit.

   

 

   

Happy coding

   

Bob

0 Likes
Anonymous
Not applicable

Any ideas on the Pioneer Kit?? i just need the schematic, so that i can build the applications on the schematic window..

   

Thank you!!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You will just need using different pin numbers for your project. The schematics and the programming are included in the project archive.

   

 

   

Bob

0 Likes