Jump to content

msp430g2231 and HC-SR04 problem

Recommended Posts


I have the old board with 2231 and tried connecting the ultrasonic sensor without success.

First, here's the code I'm using:

void setup() {
  Serial.begin ();
  pinMode(P1_5, OUTPUT);
  pinMode(P1_4, INPUT);

void loop() {
  int duration;
  int distance;
  digitalWrite(P1_5, HIGH);
  digitalWrite(P1_5, LOW);
  duration = pulseIn(P1_4, HIGH);
  distance = (duration/2) / 22; //za cm


The sensor vcc and gnd is connected on external 5v supply from the usb on the board.

The trigger pin is directly connected to P1_5.

The echo pin is connected trough 2 resistors like this




                                  |  |

                                  |  | 27Kohm   


                                   |--------------------------- P1_4


                                 |  |

                                 |  | 18Kohm


SENSOR  Echo PIn --|



So i should probably get around 2.8v output on P1_4 .

On the serial console i just get 0. 

Can someone please suggest whats seems to be wrong with this setup.(the crystal on the board is soldered if it has some importance)                       



Link to post
Share on other sites

If you're using the launchpad power, you should probably change the bottom resistor with a 15k version. The launchpad provides 3.6v and accepts vcc-0.7 as its input high. Which would be 2.9; just above your input voltage.


Just tried with 15k, no change

Link to post
Share on other sites


Your voltage divider is good. If you have 5 Volt on the HC-SR04 you should end up with 3.2 volt on P1_4 (27/15 divider).
Do you have access to a scope or a logic analyzer to look at the signal on P1_4?

No scope. I found fluke graphic multimeter but probably signal is fast to be measured. There's a ttl test with beep on it and when i make the delay between measurements 1 second i get the beep in that interval both on the echo and trig. pin. So i guess it works .

I though there's a problem in the software. Also found an arduino example with the same sensor where the 1000 microseconds is replaced with 10. Tried that an still no sucess. 

When i try to print the duration variable on the console it's also 0 so seems there's no echo signal. Hope it's not a broken module since i would have to wait a whole month for another one :(

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...