MagicCode Lesson 07: Reading an Analog Signal
MagicCode Lesson 7

Reading an Analog Signal

Read an analog Signal and display the output

Components Required

Buy

Introduction

In this example, you are learning to read an analog sensor.

Theory

In the real world, most of the signals we encounter are analog signals (temperature, air pressure, velocity), they are continuous. But computers work in the digital domain, to interact between the worlds, representing an analog signal in the digital domain is important. (to read more about analog to digital conversion, follow this link)

Methodology

For this example, we use the potentiometer on the Magicbit board, which is connected to pin, D39. It generates a voltage between 0 and 3.3V according to the angle of the potentiometer.

Magicbit inbuilt potentiometer

We read the analog signal and store it in an int type variable(0v= 0 analog value, 3.3v = 4096 analog value), sensorValue, later, we use this value to light up the red LED(D27) if the analog value exceeds 2048.

Script

https://raw.githubusercontent.com/magicbitlk/MagicCode/main/images/ex5.PNG

Explanation

This program starts by clicking the green flag. The forever block continuously runs the code held inside the block. ‘If and else’ block controls the script according to the value of the potentiometer. If the potentiometer reading is greater than 512 then, the “Set digital pin [pin] [state]” block sets the digital pin 27 to “HIGH” and else sets the digital pin 27 to “LOW”.

Note: Do the same example using the LDR on the board (D36)

Related Posts
Leave a Reply