digitalRead() in IR Sensor

Hi, @rahul.singh1 @vinayak.joshi @PPV @yeshwant.naik

I have connected a TSOP1738 IR Sensor on pin ‘4’ of the BOLT Module.
I want to read the value of the IR Sensor from pin ‘4’ and compare it with ‘1’ or ‘0’ i.e., whether it is ‘HIGH’ or ‘LOW’.
I have used the digitalRead() function to do the same, but I am not able to understand how to compare the read value with ‘0’ or ‘1’.

Following is the example of what I actually want to do.

if(digitalRead(‘4’) == 0)
{
console.log(“hi”)
}

else
{
console.log(“hello”)
}

The comparison operator is not working well for this.
Please help me on this and provide me with a solution for the same.

hey @binnymnaik30

if you print(digitalRead(your_pin_number))…you will see the output like below

O/P---------------------------------> {“success”: “1”, “value”: “1”}

Check thsi link------------------> https://docs.boltiot.com/docs/read-digital-input

And so u need to store it in a json format.

try this:

import json
response = json.loads(digitalRead(your_pin_number))
if response['value']==1:
      #STATEMENT

It should work

Yes, it worked. But I am facing another problem.

I have switched ‘ON’ a LED at pin position ‘1’. So, currently the LED is at state ‘HIGH’ and is giving off light.
Meanwhile, when I am trying to read the LED’S status using digitalRead(‘1’), the LED is going OFF on its own, i.e., the state of LED is becoming ‘LOW’ as soon as I run the digitalRead(‘1’) command at ubuntu.
Following is the code snippet:-

if(digitalRead(‘1’) == ‘1’):
{
//then some do statements;
}

Please let me know where is the problem and how to solve it.

your if statement is wrong!!..The condition would never be True as

“{“success”: “1”, “value”: “1”}” is not equal to “1”

and so the statements under if would never be executed.

And why your LED is turning OFF??..May be because of some statement after If bock{}

If you may share the screenshot of your code I can clarify much more.

oh, by mistake I have typed wrong if statement here in the doubt. Below

is the ss of the whole code.

I don’t know why but the LED is turned on but still while the execution, it is not going into the first ‘if’ and as soon as it detects the first ‘if’ the LED light is going off on its own. Also the control then goes into the second ‘if’ and I am receiving the “lights turned OFF” message through twilio.

@binnymnaik30 I am sorry I didn’t mention it before :sweat_smile: but while comparing the if condition you have to convert the json type to string type…I mean whatever the type of your other operand is E.g- integer, string etc.

Here it is a string type

Therefore Try that

#################################
if str(response[‘value’]) ==‘1’:
#################################

because ‘1’ is in string form and response['value '] is not yet string…That is why the if statement is not getting executed`

No sir, typecasting to ‘str’ also didn’t work.
The LED went off again from HIGH to LOW automatically.

Please guide what to do.

Below is the code with its output.

I see if you see the output

“Status before if is 0”…That is didgitalRead() is returning 0 and so response[‘value’] is 0 before if statement. That is why it is not going inside the if block…

Try

print(response) #before if statement to check if its success value

Yes, thats exactly where the problem is happening.
The LED is kept ON but when digitalRead() tries to fetch the status of LED, LED goes off on its own and thus digitalRead() gets 0 value.

and what did u do to make sure that your LED is ON…I mean how did u ON the LED on the first place?

By executing the following command:-

<button onclick="analogWrite(‘1’,‘225’)>ON LIGHT

I clicked this button, made sure that the light is glowing and then started executing .py file to do digitalRead(‘1’).

well I m not sure about it but I think because you used analogWrite() function to turn ON the LED that is why digitalRead() is not able to read it because it is used to read the digital input.

But I am only guessing it.

Try using digitalWrite() in place of analogWrite() to turn ON the LED and see what happens.

Also let me know because I am curious now :smile:

The onclick code for this
<button onclick="analogWrite(‘1’,‘225’)>
might not be the case if the LED is getting ON and showing the {“success”: “1”, “value”: “1”} . If the LED is getting ON and showing {“success”: “1”, “value”: “1”} then you might have used digitalWrite instead of analogWrite.

Well, my mistake again. In my code i have written digitalWrite(‘1’,‘HIGH’) only.
I wrongly typed analog here.
And with digitalWrite too its the same problem.

Yes, you are right. I have written digitalWrite() in my code.

@binnymnaik30 I think now I have an explanation why is it happening?

You see our Bolt device can only connect to one server/product Right??

And so before its linked to one of your products where you gave the command digitalWrite().

But as soon as you call the function mybolt(Api_key, Device_id) in your VMware that product gets unlinked with the device for establishing the connection with your VMware and so the device goes back to default state where there is no digitalWrite() command is issued and so the LED turns OFF.

And that is the reason why in your output the status before if is"0"

That implies that we can’t connect two products parallel. And so if you want to digitalRead() your LED then digitalWrite() it first through the code in your VM machine not by creating a product in cloud…Because what you were trying to achieve cannot be achieved.

Hardware limitaion :smile:

But It was fun the whole convo.