---
title: Problem set 5
heading: "Problem set 5: analog in and out"
eyebrow: C12 · Problem set 5
type: problems
---

::: hook
These problems use lessons 16 to 24: reading sensors with the 12-bit ADC (0 to 3.3 V maps to 0 to 4095) and driving its input, fixed-point arithmetic, filtering, PWM, driving loads, sound, and the 12-bit DAC. The clock is 10 MHz. The thermistor is 10 kΩ at 25 °C with B = 3950 K, on top of a 10 kΩ resistor.
:::

::: pool
pick: 12

::: check
kind: parts
given: V = 0.6 | 1.9 | 2.75

## A sensor puts {V} V on an ADC pin.

::: part
answer: round(V/3.3*4095)
tolerance: 0

### What code does the ADC report?

::: misses
- [any] $V/3.3 \times 4095$, rounded.
:::
:::

::: part
answer: floor((round(V/3.3*4095)*3300 + 2047)/4095)
unit: mV
tolerance: 0

### What does `(code * 3300 + 2047) / 4095` give, in integers?

::: misses
- [any] Rounded millivolts from the code.
:::
:::
:::

::: check
kind: parts
given: c = 1500 | 2700 | 3300

## The thermistor divider reads code {c}.

::: part
answer: 10*(4095 - c)/c
unit: kΩ
tolerance: 0.5%

### What's the thermistor's resistance? (V cancels: $R_T = R_f(4095 - c)/c$.)

::: misses
- [any] $R_f(3.3 - V)/V$ with V from the code.
:::
:::

::: part
answer: 1/(1/298.15 + ln((4095 - c)/c)/3950) - 273.15
unit: °C
tolerance: 0.5%

### What's the temperature?

::: misses
- [any] The Beta equation.
:::
:::
:::

::: check
kind: parts
given: s = 3 | 8 | 20; N = 16 | 100

## Single ADC readings scatter with a standard deviation of {s} codes; the program averages {N}.

::: part
answer: s/sqrt(N)
tolerance: 0.5%

### What's the scatter of the average, in codes?

::: misses
- [any] $s/\sqrt N$.
:::
:::

::: part
answer: N*10
unit: µs
tolerance: 0.5%

### How long do the conversions take, at 10 µs each?

::: misses
- [any] {N} × 10 µs.
:::
:::
:::

::: check
kind: parts
given: x = 0.375 | 2.6 | -1.125; n = 8 | 12

## A value {x} is stored in a Q format with {n} fraction bits.

::: part
answer: round(x*2^n)
tolerance: 0

### What integer stores it?

::: misses
- [any] $\text{round}(x \cdot 2^n)$.
:::
:::

::: part
answer: 2^(-n)
tolerance: 0.5%

### What's the format's resolution?

::: misses
- [any] One unit is $2^{-n}$.
:::
:::
:::

::: check
kind: parts
given: x = 1400 | 1450 | 1500 | 1600 | 1700 | 1800; x0 = 1357; x1 = 1817; y0 = 100; y1 = 200

## A table maps code {x0} to {y0} tenths of a degree and {x1} to {y1}.

::: part
answer: y0 + floor((x - x0)*(y1 - y0)/(x1 - x0))
tolerance: 0

### What does code {x} interpolate to, in integer arithmetic?

::: misses
- [any] Multiply before dividing; integer division truncates.
:::
:::

::: part
answer: (x1 - x0)*(y1 - y0)
tolerance: 0

### What's the largest intermediate product the calculation can reach in this segment?

::: misses
- [any] The widest difference times {y1} − {y0}.
:::
:::
:::

::: check
kind: parts
given: f = 1000 | 25000; D = 30 | 65; V = 5 | 12

## A motor runs from {V} V through a transistor switched by PWM at {f} Hz and {D}% duty.

::: part
answer: D/100*V
unit: V
tolerance: 0.5%

### What average voltage does it see?

::: misses
- [any] $DV$.
:::
:::

::: part
answer: 1e7/f*D/100
tolerance: 0

### What DUTY register value gives that, with PERIOD set for {f} Hz?

::: misses
- [any] PERIOD × duty.
:::
:::
:::

::: check
kind: parts
given: f = 500 | 20000 | 100000

## PWM must run at {f} Hz.

::: part
answer: 1e7/f
tolerance: 0

### What PERIOD does it need?

::: misses
- [any] $f_\text{clk}/f$.
:::
:::

::: part
answer: log(1e7/f)/log(2)
tolerance: 0.5%

### How many bits of duty resolution does that leave?

::: misses
- [any] $\log_2(\text{PERIOD})$.
:::
:::
:::

::: check
kind: parts
given: n = 55 | 67 | 74

## A tune needs MIDI note {n}.

::: part
answer: 440*2^((n - 69)/12)
unit: Hz
tolerance: 0.1%

### What's its frequency?

::: misses
- [any] $440 \times 2^{(n-69)/12}$.
:::
:::

::: part
answer: round(1e7/(440*2^((n - 69)/12)))
tolerance: 0

### What PWM PERIOD plays it?

::: misses
- [any] $f_\text{clk}/f$, rounded.
:::
:::
:::

::: check
kind: parts
given: B = 80 | 132; k = 0.25 | 1.5; g = 20 | 40

## A melody at {B} beats per minute has a note of {k} beats, followed by a {g} ms gap that's part of the note's time.

::: part
answer: 60/B*k*1000
unit: ms
tolerance: 0.5%

### How long is the note's whole slot?

::: misses
- [any] $60k/B$ s.
:::
:::

::: part
answer: 60/B*k*1000 - g
unit: ms
tolerance: 0.5%

### How long does the tone actually sound?

::: misses
- [any] The slot minus the gap.
:::
:::
:::

::: check
kind: parts
given: N = 32 | 64 | 100; fs = 8000 | 20000

## A {N}-sample waveform table is played at {fs} samples per second.

::: part
answer: fs/N
unit: Hz
tolerance: 0.5%

### What frequency results?

::: misses
- [any] $f_s/N$.
:::
:::

::: part
answer: 1e7/fs - 1
tolerance: 0

### With PRESCALE = 0, what timer TOP sets that sample rate?

::: misses
- [any] Cycles per sample, minus 1.
:::
:::
:::

::: check
kind: parts
given: f = 60 | 261.6 | 1000; fs = 10000 | 16000

## A phase accumulator updates at {fs} Hz.

::: part
answer: round(f/fs*2^32)
tolerance: 1e-6%

### What 32-bit step gives {f} Hz?

::: misses
- [any] $(f/f_s) 2^{32}$.
:::
:::

::: part
answer: fs/2^32*1e6
unit: µHz
tolerance: 0.5%

### What's its frequency resolution?

::: misses
- [any] $f_s/2^{32}$.
:::
:::
:::

::: check
kind: parts
given: m = 400 | 1650 | 3100

## The DAC must output {m} mV.

::: part
answer: floor((m*4095 + 1650)/3300)
tolerance: 0

### What does `(m * 4095 + 1650) / 3300` give, in integers?

::: misses
- [any] Scale by 4095/3300, rounding.
:::
:::

::: part
answer: floor((m*4095 + 1650)/3300)/4095*3300
unit: mV
tolerance: 0.01%

### What voltage does that code actually produce?

::: misses
- [any] code/4095 × 3300 mV.
:::
:::
:::

::: check
kind: parts
given: R = 10 | 47 | 100; C = 10 | 20; t = 2 | 10

## A {R} kΩ source drives an ADC that samples onto {C} pF through 1 kΩ, with {t} µs of acquisition time.

::: part
answer: (R + 1)*1000*C*1e-12*ln(8192)*1e6
unit: µs
tolerance: 0.5%

### What acquisition time does 12-bit settling need?

::: misses
- [any] $9.01(R_s + R_\text{sw})C_s$.
:::
:::

::: part
answer: step(t - (R + 1)*1000*C*1e-12*ln(8192)*1e6)
tolerance: 0

### Is {t} µs enough (1 for yes)?

::: misses
- [any] Compare.
:::
:::
:::

::: check
kind: parts
given: V = 12 | 24; R = 50 | 120; b = 10 | 20

## An NPN switches a {V} V, {R} Ω solenoid from a 3.3 V pin with a forced β of {b}; the pin may source 8 mA.

::: part
answer: V/R/b*1000
unit: mA
tolerance: 0.5%

### What base current does it need?

::: misses
- [any] $I_C/\beta_\text{forced}$.
:::
:::

::: part
answer: step(8 - V/R/b*1000)
tolerance: 0

### Can the pin supply it (1 for yes)?

::: misses
- [any] Compare with 8 mA.
:::
:::
:::

::: check
kind: parts
given: N = 8 | 32; s = 5 | 12; k = 3 | 5

## Readings with {s} codes of rms noise pass through either a {N}-sample moving average or a smoother with $\alpha = 2^{-\given{k}}$.

::: part
answer: s/sqrt(N)
unit: codes
tolerance: 0.5%

### How much noise does the moving average leave?

::: misses
- [any] $\sigma/\sqrt N$.
:::
:::

::: part
answer: s*sqrt(2^(-k)/(2 - 2^(-k)))
unit: codes
tolerance: 0.5%

### And the smoother?

::: misses
- [any] $\sigma\sqrt{\alpha/(2 - \alpha)}$.
:::
:::
:::
:::
