Ultrasonic Sensor not working with Attiny 13a

I am using Attiny 13a microcontroller for measuring the distance using ultrasonic sensor HC-SR04 but it is not working. Even I could not get any values in serial monitor. I am using the same code with nano it is working fine. Please can anybody help me on this.

The following is the code I have used:

const int trigPin =0;
const int echoPin =1;
const int RelayTrig = 2;
int PumpON_distance = 24; // inches t
int PumpOFF_distance = 8; //
void setup() {

pinMode (RelayTrig, OUTPUT);
}
void loop() {
long duration, inches;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);

if (inches >= PumpON_distance ) {
digitalWrite(RelayTrig, HIGH); //Switch ON pump
}

if (inches <= PumpOFF_distance ) {
digitalWrite(RelayTrig, LOW); //Switch OFF pump
}

delay(3000);
}
long microsecondsToInches(long microseconds) {
return (microseconds / 74) / 2;
}

What core are you using with the ATtiny13?

Are you using the correct pin for pulseIn()? Checked whether micros() is enabled?

Please edit your post to add code tags.

Thank u for ur reply,

I am using Microcore.
Used pin 0 for Trigger and 1 for Echo
micros() is disabled

Now often, I am getting the following error. Even I could not abled to load blink project

avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.

micros() is disabled

micros() is required by pulseIn(). Always check the docs.

Thanks for your valuable time,

Now , I am getting the following error. I could not upload any sketch(Even Blink example).

avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.

Actually MicroController was worked for Blink Project . After I am trying to upload a different sketck for working with utrasonic sensor, I changed some settings like the clock to 9.6 Mhz and enabled micros. after that it started showing above stated error. But my last updated blink Project is working still now. if I try to upload any new project, even blink project its not getting uploaded

svaradha:
Double check connections

Assuming you have selected the correct processor in the IDE and did not destroy the ATtiny in any way, this is very likely where you have to look for a solution.

I verified the connection many times, it looks perfect. Is there a chance for my nano not working? but if I upload a sketch to a nano it is working.

Then grab another ATtiny chip... see if the problem is there.

The Nano seems to be working as it should, it gives the signature check response back to the IDE.

Now I changed the Micro Controller to ATMega8 . Now my code uploaded successfully but my utlrasonic sensor not working. I used internaI clock speed of 8Mhz .
I am posting code below:

const int trigPin = 11;
const int echoPin = 12;
const int RelayTrig = 2;
int PumpON_distance = 12; // inches
int PumpOFF_distance = 6; //
void setup() {
Serial.begin(9600);
pinMode (RelayTrig, OUTPUT);
pinMode (A4, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {

digitalWrite(A4, HIGH); //Light for testing
delay(1000);
digitalWrite(A4, LOW); //Light for testing

long duration, inches;

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);

if (inches >= PumpON_distance ) {
digitalWrite(RelayTrig, HIGH); //Switch ON pump
}

if (inches <= PumpOFF_distance ) {
digitalWrite(RelayTrig, LOW); //Switch OFF pump
}

delay(3000);
}
long microsecondsToInches(long microseconds) {

return (microseconds / 74) / 2;
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.