MOCKSTACKS
EN
Questions And Answers

More Tutorials









Raspberry Turn an LED On and Off with a Rotary Angle Sensor


First, let's try to write a simple program to turn on the LED when the Rotary Angle Sensor rotates through the center point.

Hardware Connection

• Raspberry Pi Pico

• Grove Shield for Pi Pico

• Grove - LED Pack

• Grove - Rotary Angle Sensor


Plug the Pico and the Shield, use a Grove data cable to connect the LED to D16, and connect the Rotary Angle Sensor to A1.

Write a Program

Similarly, at the beginning of the program, import the functions that need to be used in the machine library, and define pins:

1 from machine import ADC, Pin 
2 from time import sleep 
3
4 LED = Pin(16, Pin. OUT) 
5 ROTARY_ANGLE_SENSOR = ADC(1)

Then use the “if" statement to judge the condition. When the return value of the Rotary Angle Sensor is greater than 30000, the LED turns on; otherwise, the LED turns off.

1 from machine import ADC, Pin 
2 from time import sleep 
3 
4 LED - Pin(16, Pin.OUT) 
5 ROTARY_ANGLE_SENSOR = ADC (1) 
6 while True: 
7 print(ROTARY_ANGLE_SENSOR.read_u16() )
8 if ROTARY_ANGLE_SENSOR.read_u16) > 30000:
9 LED.value(1)
10 sleep(1) 
11 else:
12 LED.value (0) 13 sleep(1)

In the "if" statement, we use the comparison operator ">" to compare the relationship between the analog value read from the Rotary Angle Sensor and the 30000 threshold.

You can test the effect of the program by changing the comparison operator used in the "if" statement. Note that if we use "==" to write expressions in the program, it is difficult to achieve the effect we want. This is because the value range of analog value is wide. When we rotate the knob, it is difficult to turn it to a specific value to fulfill the condition of the expression.

In addition to using the comparison operator to complete a single expression statement, we can also use the logical operator to concatenate two independent statements to control the LED to light only within a specific value range:

1 from machine import ADC, Pin 
2 from time import sleep 
3
4 LED = Pin(16, Pin. OUT) 
5 ROTARY_ANGLE_SENSOR = ADC(1) 
6 while True:
7 print(ROTARY_ANGLE_SENSOR.read_u16 )
8 if ROTARY_ANGLE_SENSOR.read_u16) > 20000 and ROTARY_ANGLE_SENSOR.read_u16) < 40000:
9 LED.value(1)
10 sleep(1) 
11 else:
12 LED.value () 13 sleep(1)



Conclusion

In this page (written and validated by ) you learned about Raspberry Turn an LED On and Off with a Rotary Angle Sensor . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Analog Signal in Circuit 2 the Use of PWM.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.